自然界中的相关数据
给定数组 grains,其中包含一组谷物样本的宽度和长度。您怀疑宽度与长度是相关的。为验证这一点,请绘制宽度与长度的散点图,并计算它们的皮尔逊相关系数。
本练习是课程的一部分
Python 中的无监督学习
练习说明
- 导入:
- 将
matplotlib.pyplot导入为plt。 - 从
scipy.stats导入pearsonr。
- 将
- 将
grains的第 0 列赋给width,将第 1 列赋给length。 - 绘制散点图:x 轴为
width,y 轴为length。 - 使用
pearsonr()函数计算width与length的皮尔逊相关系数。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Perform the necessary imports
____
____
# Assign the 0th column of grains: width
width = ____
# Assign the 1st column of grains: length
length = ____
# Scatter plot width vs length
plt.scatter(____, ____)
plt.axis('equal')
plt.show()
# Calculate the Pearson correlation
correlation, pvalue = ____
# Display the correlation
print(correlation)