穀物資料的階層式分群
在影片中,你學到 SciPy 的 linkage() 函式會對樣本陣列執行階層式分群。請使用 linkage() 對穀物樣本做階層式分群,並用 dendrogram() 視覺化結果。部分穀物量測資料已在陣列 samples 中提供,各樣本的品種則在清單 varieties 中提供。
本練習屬於課程
Unsupervised Learning in Python
練習說明
- 匯入:
- 從
scipy.cluster.hierarchy匯入linkage與dendrogram。 - 將
matplotlib.pyplot以別名plt匯入。
- 從
- 使用
linkage()搭配關鍵字引數method='complete',對samples進行階層式分群。將結果指定給mergings。 - 以
dendrogram()針對mergings繪製樹狀圖,並指定關鍵字引數labels=varieties、leaf_rotation=90、leaf_font_size=6。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Perform the necessary imports
from ____ import ____, ____
import ____ as ____
# Calculate the linkage: mergings
mergings = ____
# Plot the dendrogram, using varieties as labels
dendrogram(____,
labels=____,
leaf_rotation=____,
leaf_font_size=____,
)
plt.show()