開始使用免費開始

用 PCA 讓穀物量測去相關化

你在前一個練習中觀察到,穀物的寬度與長度量測彼此相關。現在,你將使用 PCA 來讓這些量測去相關化,接著繪製去相關後的點,並計算它們的皮爾森相關係數。

本練習屬於課程

Unsupervised Learning in Python

檢視課程

練習說明

  • sklearn.decomposition 匯入 PCA
  • 建立名為 modelPCA 實例。
  • 使用 model.fit_transform() 方法將 PCA 轉換套用到 grains。把結果指定給 pca_features
  • 之後用來擷取、繪圖並計算 pca_features 前兩欄皮爾森相關的程式碼已經替你寫好,按下「送出答案」即可查看結果!

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import PCA
____

# Create PCA instance: model
model = ____

# Apply the fit_transform method of model to grains: pca_features
pca_features = ____

# Assign 0th column of pca_features: xs
xs = pca_features[:,0]

# Assign 1st column of pca_features: ys
ys = pca_features[:,1]

# Scatter plot xs vs ys
plt.scatter(xs, ys)
plt.axis('equal')
plt.show()

# Calculate the Pearson correlation of xs and ys
correlation, pvalue = pearsonr(xs, ys)

# Display the correlation
print(correlation)
編輯並執行程式碼