2022-07-21

sil = []

for i in range(2,11):

    kmeans= KMeans(n_clusters=i,init='k-means++',random_state=123) # 分成2-10群,分别计算silhouette值,画图

    y_kmeans = kmeans.fit_predict(target_value.reshape(-1,1))

    sil.append(metrics.silhouette_score(target_value.reshape(-1,1),y_kmeans))

    print(i)

plt.plot(range(2,11),sil)

plt.title('The Silhouette Method')

plt.xlabel('Number of clusters')

plt.ylabel('Silhouette Coefficient')

plt.show()

你可能感兴趣的:(2022-07-21)