寶可夢目擊:k-means 分群
我們要延續上一個練習,繼續調查傳說寶可夢的目擊地點。和前一題一樣,使用相同的寶可夢目擊資料。這個練習中,你會用 k-means 分群將這些目擊點分成數個群組。
x 和 y 是目擊地點的 X 與 Y 座標欄位,存放在 pandas 的 DataFrame df 中。以下模組可供使用:matplotlib.pyplot 作為 plt、seaborn 作為 sns,以及 pandas 作為 pd。
本練習屬於課程
Python 中的叢集分析
練習說明
- 匯入
kmeans與vq函式。 - 使用
kmeans(),並設定為 2 個叢集,來計算叢集中心。 - 使用
vq()函式將每個資料點指派叢集標籤。 - 使用 seaborn 繪製散點圖,並讓不同叢集以不同顏色顯示
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import kmeans and vq functions
from scipy.cluster.vq import ____, ____
# Compute cluster centers
centroids,_ = ____(____, ____)
# Assign cluster labels
df['cluster_labels'], _ = ____(____, ____)
# Plot the points with seaborn
sns.scatterplot(x=____, y=____, hue=____, data=df)
plt.show()