自然界中的相關資料
給你一個陣列 grains,其中包含穀物樣本的寬度與長度。你懷疑寬度與長度之間具有相關性。為了驗證這點,請繪製寬度對長度的散佈圖,並計算它們的 Pearson 相關係數。
本練習屬於課程
Unsupervised Learning in Python
練習說明
- 匯入:
- 將
matplotlib.pyplot匯入為plt。 - 從
scipy.stats匯入pearsonr。
- 將
- 將
grains的第0欄指定給width,第1欄指定給length。 - 繪製散佈圖,x 軸放
width,y 軸放length。 - 使用
pearsonr()函式計算width與length的 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)