无法在Kmeans,Sklearn中将标签与质心相关联

时间:2015-08-19 04:46:34

标签: python numpy scikit-learn

如何知道哪个标签属于哪个质心?下面的代码生成标签和质心。

import numpy as np
from sklearn.cluster import MiniBatchKMeans

data = np.array([[1,2,3,4,5,0,0],
                 [0,0,6,7,8,9,10],
                 [11,12,13,14,15,0,0]])             
x,y = np.shape(data)
data_to_cluster = np.reshape(data, (x*y, 1))

km = MiniBatchKMeans(n_clusters=3, n_init=10, max_iter=5)
km.fit(data_to_cluster)

labels = km.labels_
centers = km.cluster_centers_

1 个答案:

答案 0 :(得分:1)

我猜,这是你想要的: 获取与点标签关联的中心,然后将其重新整形为数据形状。

output = centers[labels]
output = np.reshape(output, data.shape)