我在一个矩阵中有多个点,我试图使用Python中的“scikit-learn”库使用K-means算法对其进行分类。以下是我迄今所做的工作:
k_means = KMeans(init='k-means++', k=5, n_init=10)
k_means.fit(X) ## X is the matrix containing the data
... # some code to plot
结果如下:
现在我想得到在“红色”集群中的点,例如。我怎样才能用sklearn
做到这一点呢?
发布于 2014-11-17 07:22:38
我想我找到了。它很简单:对象km
有几个属性。在这些属性中,有labels
,它为集群所属的每一个点提供了信息:
km_labels = km.labels_
#to get all the items in the first cluster just get the indexes int km_labels with the
#value = 1
index = [x[0] for x, value in numpy.ndenumerate(km_labels) if value==1]
发布于 2014-11-17 07:03:17
KMeans是一个公式,而不是数据库。我的意思是,它不为每个记录保留一个值的记录,而是记录一个函数,该函数将再次分配给定输入的集群。为了获得聚类分配,有一种叫做“预测”的方法。
kmeans.predict(value)
这将为您提供来自kmeans的集群分配。如果你迭代你的观点,你应该能够得到它们。
https://stackoverflow.com/questions/26975446
复制