如何在Matplotlib Python中绘制每个群集的平均轮廓分数

时间:2019-08-01 03:43:39

标签: matplotlib silhouette

我有78行和131列,我需要在python matplotlib中以线图的形式绘制每个群集的平均轮廓分数。我做了这些代码,效果很好,但我不知道如何绘制?

mean = KMeans(n_clusters = 2)
kmean.fit(Data1)
centroids = kmean.cluster_centers_
print("Shape of Centroids Array: " + str(centroids.shape))

print()
print(centroids)

from collections import Counter
labels = kmean.labels_
c = Counter(labels)
print(c.most_common())

Record_array =Data1.values
print(Record_array)
mean_sihouette_score = ss(Record_array, labels)
print(mean_sihouette_score)

for cluster_number in range(0,2):

    print("Cluster {} contains {} samples with percentage of {:.2f}%".format(cluster_number, c[cluster_number], c[cluster_number]/sum(c.values()) *100))

1 个答案:

答案 0 :(得分:1)

编码以绘制平均轮廓分数并非易事。 我通常已经做过黄砖了。

在这里,您可以看一下。

https://www.scikit-yb.org/en/latest/api/cluster/silhouette.html

或其代码(请事先安装yellowbrick)

from yellowbrick.cluster import SilhouetteVisualizer

model = KMeans(5, random_state=42)
visualizer = SilhouetteVisualizer(model, colors='yellowbrick')

visualizer.fit(X)        # Fit the data to the visualizer
visualizer.show()        # Finalize and render the figure