开始使用免费开始使用

鱼类测量数据的降维

在之前的练习中,您已经看到对于鱼类测量数据来说,"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)
编辑并运行代码