使用 matplotlib 視覺化叢集
我們之前提到,視覺化對於評估形成的叢集與發現資料中的趨勢很重要。現在讓我們用 matplotlib 模組來視覺化 Comic-Con 的人流資料集。
資料儲存在 pandas 的 DataFrame comic_con 中。x_scaled 與 y_scaled 是在某一時間點人群標準化後的 X 與 Y 座標的欄位名稱。cluster_labels 包含叢集標籤。連結(linkage)物件儲存在變數 distance_matrix 中。
本練習屬於課程
Python 中的叢集分析
練習說明
- 從
matplotlib模組匯入pyplot並命名為plt。 - 為兩個叢集標籤
1與2定義一個colors字典。 - 畫出散佈圖,並依據
colors字典為各叢集設定顏色。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the pyplot class
____
# Define a colors dictionary for clusters
colors = {____:'red', ____:'blue'}
# Plot a scatter plot
comic_con.plot.scatter(x=____,
y=____,
c=comic_con['cluster_labels'].apply(____))
plt.show()