在Cluster Similarity Kmeans中

时间:2018-03-17 07:14:01

标签: python cluster-analysis k-means

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

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

总而言之,有没有办法让一个数字来识别" good" kmeans中的每个集群是?我们可以争辩说,善是相对的,但我们可以认为我对群内方差更感兴趣,以确定特定群集的优异程度。

1 个答案:

答案 0 :(得分:0)

使用从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)