【问题标题】:Within Cluster Similarity Kmeans聚类内相似度 Kmeans
【发布时间】:2018-08-26 06:33:57
【问题描述】:

我正在尝试在 sklearn python 中使用 kmeans 对二维用户数据进行聚类。我使用肘部方法(集群编号的增加不会导致平方误差总和显着下降的点)来确定正确的编号。簇数为 50。

应用 kmeans 后,我希望了解每个集群内数据点的相似性。由于我有 50 个集群,有没有办法获得一个数字(类似于每个集群内的差异),这可以帮助我了解每个集群内的接近程度或数据点。像 0.8 这样的数字意味着每个集群中的记录具有很大的差异,而 0.2 意味着它们密切“相关”。

总而言之,有没有办法获得一个数字来确定 kmeans 中每个集群的“好”程度?我们可以说好是相对的,但让我们考虑一下,我对集群内的方差更感兴趣,以确定特定集群的好坏。

【问题讨论】:

  • 关于聚类有两个相似之处:簇间相似度簇内相似度 簇间:簇之间,应该是高的集群:集群内应该很小我建议查看en.wikipedia.org/wiki/Silhouette_(clustering)以进一步阅读和理解
  • 感谢 Shahaf。我已经看到 silhoutte 系数被用于识别 kmeans 的 k 值,但在找到“理想”k 后,您能指导我如何在每个集群上使用 silhoutte 吗?一个python代码真的很有帮助..
  • 我也面临同样的问题,我正在使用轮廓分数来找到最佳的 K 个集群,正如您提到的,轮廓方法可用于计算每个样本的相似度,就像 scikit-learn.org/stable/modules/generated/…
  • 簇内平方和方差不限于0:1。自己计算这些值很简单,但它们不会很有用。

标签: python cluster-analysis k-means


【解决方案1】:

使用来自https://plot.ly/scikit-learn/plot-kmeans-silhouette-analysis/的剪影分数的代码示例

from __future__ import print_function

from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_samples, silhouette_score

# Generating the sample data from make_blobs
# This particular setting has one distinct cluster and 3 clusters placed close
# together.
X, y = make_blobs(n_samples=500,
                  n_features=2,
                  centers=4,
                  cluster_std=1,
                  center_box=(-10.0, 10.0),
                  shuffle=True,
                  random_state=1)  # For reproducibility

range_n_clusters = [2, 3, 4, 5, 6]

for n_clusters in range_n_clusters:
  # Initialize the clusterer with n_clusters value and a random generator
  # seed of 10 for reproducibility.
  clusterer = KMeans(n_clusters=n_clusters, random_state=10)
  cluster_labels = clusterer.fit_predict(X)
  print(cluster_labels)
  # The silhouette_score gives the average value for all the samples.
  # This gives a perspective into the density and separation of the formed
  # clusters
  silhouette_avg = silhouette_score(X, cluster_labels)
  print("For n_clusters =", n_clusters,
        "The average silhouette_score is :", silhouette_avg)

  # Compute the silhouette scores for each sample
  sample_silhouette_values = silhouette_samples(X, cluster_labels)

【讨论】:

  • 谢谢@shahaf .. silhouette_samples 是我一直在寻找的功能
猜你喜欢
  • 2012-06-24
  • 2021-10-28
  • 2013-07-15
  • 2015-07-17
  • 2011-08-16
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
相关资源
最近更新 更多