I try to cluster a dataframe of 227 rows in 5 clusters using kmeans algorithm. Each time I run my code I got different labels and different clusters which make my analysis afterwards a bit tricky.

Someone told me to use the parameter: randomstate to have a reproductility in my results. I did. I have the same clusters but still not the same label. Is it normal? Is there a way to get the same labels ?

below my code:

Test sur 5 clusters

# Data
X = df.iloc[:,1:]
myseed = 10

# Modèle kmeans à 5 clusters
km = KMeans(n_clusters=5, random_state=myseed, n_init=30) 

# Fitting du modèle aux points 
km = km.fit(X)
y_km = km.predict(X)
  • $\begingroup$ It's important to note that a cluster assignment is not a 'label'; what "cluster 3" means in your clustering is arbitrary, and is not comparable to 'cluster 3' from any other clustering necessarily. You can try to compare clusters but need to some other way define which ones are comparable. $\endgroup$ – Sean Owen May 17 '20 at 15:08
  • $\begingroup$ lut[idx] = np.arange raises the following error: int() argument must be a string, a bytes-like object or a number, not 'builtin_function_or_method' $\endgroup$ – Nickie Aug 11 '20 at 15:07

Unfortunately, there isn't a built-in option to do it. Each time you run K-Means, the labels are assigned randomly. Even if you state the same random seed. However, based on this answer in StackOverFlow, you can create a lookup table and run it after your K-Means.

from sklearn.cluster import KMeans
k = 5
kmeans = KMeans(n_clusters=k, random_state=0).fit(X)

idx = np.argsort(kmeans.cluster_centers_.sum(axis=1))
lut = np.zeros_like(idx)
lut[idx] = np.arange

With this, you will always have the same output as

In [73]: kmeans.labels_
Out[73]: array([1, 4, 1, ..., 0, 1, 0])

In [74]: lut[kmeans.labels_]
Out[74]: array([3, 0, 3, ..., 2, 4, 2], dtype=int64

When lut[kmeans.labels_] = 0, then you always have the smallest cluster and with lut[kmeans.labels_] = 4 you have the largest cluster.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.