无法在 Kmeans、Sklearn 中 link 带有质心的标签
Could not link labels with centroids in Kmeans,Sklearn
如何知道哪个标签属于哪个质心?下面的代码产生了标签和质心。
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_
我猜,这就是你想要的:
获取与点标签关联的中心,然后将其整形为数据形状。
output = centers[labels]
output = np.reshape(output, data.shape)
如何知道哪个标签属于哪个质心?下面的代码产生了标签和质心。
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_
我猜,这就是你想要的: 获取与点标签关联的中心,然后将其整形为数据形状。
output = centers[labels]
output = np.reshape(output, data.shape)