第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也是这样做

把参数改改就好

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 黑白,是我给世界的单纯, 深夜,总有和孤独对抗的人。
    青青子衿0917阅读 201评论 0 0
  • 傍晚接到妈妈的一条qq消息,让我又犯起头疼来:买菜去,现在,马上,去晚了鹅肝就卖完了。 我挎起...
    69659973ba53阅读 474评论 0 1
  • 我看你 红了鼻 湿了眼 你却说 感冒了 。。。
    花期渐远阅读 173评论 0 0
  • 你是哪种考研人?下面我们通过一个小小的心理测试,只需1分钟,帮助大家来了解自我,以便能够扬长补短,提高考研力。 1...
    smile_eye阅读 366评论 0 0

友情链接更多精彩内容