开始使用免费开始使用

自然界中的相关数据

给定数组 grains,其中包含一组谷物样本的宽度和长度。您怀疑宽度与长度是相关的。为验证这一点,请绘制宽度与长度的散点图,并计算它们的皮尔逊相关系数。

本练习是课程的一部分

Python 中的无监督学习

查看课程

练习说明

  • 导入:
    • matplotlib.pyplot 导入为 plt
    • scipy.stats 导入 pearsonr
  • grains 的第 0 列赋给 width,将第 1 列赋给 length
  • 绘制散点图:x 轴为 width,y 轴为 length
  • 使用 pearsonr() 函数计算 widthlength 的皮尔逊相关系数。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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)
编辑并运行代码