鱼类测量数据的降维
在之前的练习中,您已经看到对于鱼类测量数据来说,"intrinsic dimension"(内在维度)取 2 是一个合理的选择。现在请使用 PCA 对该数据进行降维,只保留最重要的 2 个主成分。
鱼类测量数据已经为您完成了缩放,可直接通过 scaled_samples 使用。
本练习是课程的一部分
Python 中的无监督学习
练习说明
- 从
sklearn.decomposition导入PCA。 - 创建名为
pca的 PCA 实例,并设置n_components=2。 - 使用
pca的.fit()方法,将其拟合到已缩放的鱼类测量数据scaled_samples。 - 使用
pca的.transform()方法,对scaled_samples进行变换。将结果赋值给pca_features。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import PCA
____
# Create a PCA model with 2 components: pca
pca = ____
# Fit the PCA instance to the scaled samples
____
# Transform the scaled samples: pca_features
pca_features = ____
# Print the shape of pca_features
print(pca_features.shape)