開始使用免費開始

魚類量測值的降維

在前一個練習中,你看到對於魚類量測值來說,「內在維度」取 2 是合理的選擇。現在請用 PCA 來對魚類量測值進行降維,只保留最重要的 2 個主成分。

魚類量測值已替你完成縮放,可直接使用 scaled_samples

本練習屬於課程

Unsupervised Learning in 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)
編輯並執行程式碼