開始使用免費開始

使用 matplotlib 視覺化叢集

我們之前提到,視覺化對於評估形成的叢集與發現資料中的趨勢很重要。現在讓我們用 matplotlib 模組來視覺化 Comic-Con 的人流資料集。

資料儲存在 pandas 的 DataFrame comic_con 中。x_scaledy_scaled 是在某一時間點人群標準化後的 X 與 Y 座標的欄位名稱。cluster_labels 包含叢集標籤。連結(linkage)物件儲存在變數 distance_matrix 中。

本練習屬於課程

Python 中的叢集分析

檢視課程

練習說明

  • matplotlib 模組匯入 pyplot 並命名為 plt
  • 為兩個叢集標籤 12 定義一個 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()
編輯並執行程式碼