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)