始める無料で始める

PCA による穀粒測定値の相関除去

前の演習で、穀粒の幅と長さの測定値に相関があることを確認しました。ここでは、PCA を使ってこれらの測定値の相関を取り除き、相関除去後の点をプロットして、Pearson の相関係数を計算します。

この演習はコースの一部です

Pythonで学ぶ教師なし学習

コースを見る

演習の手順

  • sklearn.decomposition から PCA をインポートします。
  • model という名前で PCA のインスタンスを作成します。
  • model.fit_transform() メソッドを使って grains に PCA 変換を適用し、結果を pca_features に代入します。
  • 最初の 2 列である pca_features を取り出してプロットし、Pearson の相関を計算する後続のコードは用意されています。結果を見るために Submit を押してください!

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# 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)
コードを編集して実行