開始使用免費開始

階層式分群:ward 方法

又到 Comic-Con 的時間了!Comic-Con 是在全球主要城市舉辦的年度漫畫盛會。你手上有去年的人流資料,也就是在特定時間點會場內的人數。你希望決定攤位的位置,讓銷售最大化。請使用 ward 方法進行階層式分群,找出該區域中的兩個熱門聚集點。

資料已存於 pandas 的 DataFrame comic_conx_scaledy_scaled 是在特定時間點,人群標準化後的 X 與 Y 座標欄位名稱。

本練習屬於課程

Python 中的叢集分析

檢視課程

練習說明

  • scipy.cluster.hierarchy 匯入 fclusterlinkage
  • linkage() 函式中使用 ward 方法。
  • distance_matrix 形成 2 個平面叢集,並指派叢集標籤。
  • 「執行繪圖程式碼來查看結果。」

動手互動練習

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

# Import the fcluster and linkage functions
from scipy.cluster.hierarchy import ____, ____

# Use the linkage() function
distance_matrix = ____(comic_con[['x_scaled', 'y_scaled']], ____ = ____, metric = 'euclidean')

# Assign cluster labels
comic_con['cluster_labels'] = ____(____, ____, criterion='maxclust')

# Plot clusters
sns.scatterplot(x='x_scaled', y='y_scaled', 
                hue='cluster_labels', data = comic_con)
plt.show()
編輯並執行程式碼