Roman
Roman

Reputation: 3241

Could not link labels with centroids in Kmeans,Sklearn

How to know which label belongs to what centroids? The code below has produces the labels and centroids.

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_

Upvotes: 0

Views: 532

Answers (1)

Hugo
Hugo

Reputation: 153

I guess, it is what you want: Get the center associated to the label of the points and then reshape it to the data shape.

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

Upvotes: 1

Related Questions