第2大題剩下的

第3小問 Elbow method

k = 5

plt.figure(figsize=(10,5))

plt.plot()

plt.scatter(product_df[:, 0], product_df[:, 1], c='k')

plt.show()

# Fit clusters for various numbers of clusters

from sklearn.cluster import KMeans

K = range(1,10)

KM = [KMeans(n_clusters=k, random_state=0).fit(product_df) for k in K]

centroids = [km.cluster_centers_ for km in KM]? # cluster centroids

# Compute average euclidean distance between each point and its cluster centroid

from scipy.spatial.distance import cdist

D_k = [cdist(product_df, cent, 'euclidean') for cent in centroids]

cIdx = [np.argmin(D,axis=1) for D in D_k]

dist = [np.min(D,axis=1) for D in D_k]

avgWithinSS = [sum(d)/product_df.shape[0] for d in dist]

#Plot it

plt.plot()

plt.plot(K, avgWithinSS, 'b.-', ms=10)

plt.xlabel('Number of clusters')

plt.ylabel('Average within-cluster squared error')

plt.show()


SpectralClustering也是這樣做

把參數(shù)改改就好

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 黑白,是我給世界的單純, 深夜,總有和孤獨(dú)對抗的人。
    青青子衿0917閱讀 201評論 0 0
  • 傍晚接到媽媽的一條qq消息,讓我又犯起頭疼來:買菜去,現(xiàn)在,馬上,去晚了鵝肝就賣完了。 我挎起...
    69659973ba53閱讀 474評論 0 1
  • 我看你 紅了鼻 濕了眼 你卻說 感冒了 。。。
    花期漸遠(yuǎn)閱讀 173評論 0 0
  • 你是哪種考研人?下面我們通過一個小小的心理測試,只需1分鐘,幫助大家來了解自我,以便能夠揚(yáng)長補(bǔ)短,提高考研力。 1...
    smile_eye閱讀 366評論 0 0

友情鏈接更多精彩內(nèi)容