【问题标题】:kmeans clustering : how to access cluster datapointskmeans 集群:如何访问集群数据点
【发布时间】:2017-06-01 21:10:11
【问题描述】:

这是我从 kmeans scikit 文档和讨论 kmeans 的博客文章中汇总的 kmeans 算法的实现:

#http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html
#http://fromdatawithlove.thegovans.us/2013/05/clustering-using-scikit-learn.html

from sklearn.cluster import KMeans
import numpy as np
from matplotlib import pyplot

X = np.array([[10, 2 , 9], [1, 4 , 3], [1, 0 , 3],
               [4, 2 , 1], [4, 4 , 7], [4, 0 , 5], [4, 6 , 3],[4, 1 , 7],[5, 2 , 3],[6, 3 , 3],[7, 4 , 13]])
kmeans = KMeans(n_clusters=3, random_state=0).fit(X)

k = 3
kmeans.fit(X)

labels = kmeans.labels_
centroids = kmeans.cluster_centers_

for i in range(k):
    # select only data observations with cluster label == i
    ds = X[np.where(labels==i)]
    # plot the data observations
    pyplot.plot(ds[:,0],ds[:,1],'o')
    # plot the centroids
    lines = pyplot.plot(centroids[i,0],centroids[i,1],'kx')
    # make the centroid x's bigger
    pyplot.setp(lines,ms=15.0)
    pyplot.setp(lines,mew=2.0)
pyplot.show()

print(kmeans.cluster_centers_.squeeze())

如何打印/访问每个 k 个集群的数据点。

if k = 3 : 
cluster 1 : [10, 2 , 9], [1, 4 , 3], [1, 0 , 3]                  
cluster 2 : [4, 0 , 5], [4, 6 , 3],[4, 1 , 7],[5, 2 , 3],[6, 3 , 3],[7, 4 , 13]
cluster 3 : [4, 2 , 1], [4, 4 , 7]

读取http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html kmeans 对象上没有此属性或方法?

更新:

kmeans.labels_ 返回array([1, 0, 2, 0, 2, 2, 0, 2, 0, 0, 1], dtype=int32)

但这如何显示 3 个集群中的每个集群中的数据点?

【问题讨论】:

  • 不是方法,不是....仔细查看链接中的文档。
  • @JackManey 最接近我发现的是 print(kmeans.labels_), print(kmeans.get_params),print(kmeans.cluster_centers_) 但这些属性都没有打印集群值。
  • ...你是什么意思,确切地说是“集群值”?
  • @JackManey 我现在意识到“价值观”是模棱两可的。我所说的值是指“数据点”,我已经对此进行了更新。
  • 啊,在这种情况下,kmeans.labels_ 会为您提供每个对应数据点的集群分配(请记住,NumPy 数组的行是固定顺序的!)。

标签: python scikit-learn k-means


【解决方案1】:

如果您使用适合 KMeans 对象的 _labels 属性,您将获得每个训练向量的集群分配数组。标签数组的顺序与您的训练数据相同,因此您可以压缩它们或为每个唯一标签执行 numpy.where()。

【讨论】:

    【解决方案2】:

    要访问 k-means 聚类后的数据点:

    添加代码:

    sortedR = sorted(result, key=lambda x: x[1])
    sortedR
    

    完整代码:

        from sklearn.cluster import KMeans
        import numpy as np
        from matplotlib import pyplot
    
        X = np.array([[10, 2 , 9], [1, 4 , 3], [1, 0 , 3],
                       [4, 2 , 1], [4, 4 , 7], [4, 0 , 5], [4, 6 , 3],[4, 1 , 7],[5, 2 , 3],[6, 3 , 3],[7, 4 , 13]])
        kmeans = KMeans(n_clusters=3, random_state=0).fit(X)
    
        k = 3
        kmeans = KMeans(n_clusters=k)
        kmeans.fit(X)
    
        labels = kmeans.labels_
        centroids = kmeans.cluster_centers_
    
        for i in range(k):
            # select only data observations with cluster label == i
            ds = X[np.where(labels==i)]
            # plot the data observations
            pyplot.plot(ds[:,0],ds[:,1],'o')
            # plot the centroids
            lines = pyplot.plot(centroids[i,0],centroids[i,1],'kx')
            # make the centroid x's bigger
            pyplot.setp(lines,ms=15.0)
            pyplot.setp(lines,mew=2.0)
        pyplot.show()
    
    result = zip(X , kmeans.labels_)
    
    sortedR = sorted(result, key=lambda x: x[1])
    sortedR
    

    【讨论】:

      猜你喜欢
      • 2015-10-05
      • 2019-04-04
      • 1970-01-01
      • 2016-11-09
      • 2018-01-05
      • 2021-07-24
      • 2016-03-13
      • 2021-12-29
      • 2020-11-21
      相关资源
      最近更新 更多