開始使用免費開始

自然界中的相關資料

給你一個陣列 grains,其中包含穀物樣本的寬度與長度。你懷疑寬度與長度之間具有相關性。為了驗證這點,請繪製寬度對長度的散佈圖,並計算它們的 Pearson 相關係數。

本練習屬於課程

Unsupervised Learning in Python

檢視課程

練習說明

  • 匯入:
    • matplotlib.pyplot 匯入為 plt
    • scipy.stats 匯入 pearsonr
  • grains 的第 0 欄指定給 width,第 1 欄指定給 length
  • 繪製散佈圖,x 軸放 width,y 軸放 length
  • 使用 pearsonr() 函式計算 widthlength 的 Pearson 相關係數。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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)
編輯並執行程式碼