使用 PCA 去相关化谷物测量值
在上一个练习中,您已经观察到谷物的宽度和长度测量值是相关的。现在,您将使用 PCA 将这些测量值去相关化,然后绘制去相关后的点,并计算它们的皮尔逊相关系数。
本练习是课程的一部分
Python 中的无监督学习
练习说明
- 从
sklearn.decomposition导入PCA。 - 创建一个名为
model的PCA实例。 - 使用
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)