如何在python中分别计算每个集群的Silhouette Score [英] How to calculate the Silhouette Score for each cluster separately in python

查看:98
本文介绍了如何在python中分别计算每个集群的Silhouette Score的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以使用 1 行代码轻松提取轮廓分数,该代码对所有集群的分数求平均值,但是如何从 scikit 学习轮廓分数的实现中提取每个中间分数?我希望能够单独为每个集群提取相同的分数,而不仅仅是获得总分.

You can easily extract the silhouette score with 1 line of code that averages the scores for all your clusters but how do you extract each of the intermediate scores from the scikit learn implementation of the silhouette score? I want to be able to extract this same score for each cluster individually, not only get the total score.

metrics.silhouette_score(x, y, metric='euclidean')

推荐答案

如果您的数据看起来像这样:

If your data looks something like this:

num_clusters = 3
X, y = datasets.load_iris(return_X_y=True)
kmeans_model = KMeans(n_clusters=num_clusters, random_state=1).fit(X)
cluster_labels = kmeans_model.labels_

您可以使用 metrics.silhouette_samples 计算每个样本的轮廓系数,然后取每个簇的平均值:

You could use metrics.silhouette_samples to compute the silhouette coefficients for each sample, then take the mean of each cluster:

sample_silhouette_values = metrics.silhouette_samples(X, cluster_labels)

means_lst = []
for label in range(num_clusters):
    means_lst.append(sample_silhouette_values[cluster_labels == label].mean())

<小时>

print(means_lst)                                                                             
[0.4173199215409322, 0.7981404884286224, 0.45110506043401194] # 1 mean for each of the 3 clusters

这篇关于如何在python中分别计算每个集群的Silhouette Score的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆