如何知道哪个标签属于哪个质心?下面的代码生成了标签和质心。
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)