Machine Learning · medium

How do you choose the number of clusters in k-means?

Asked in Data Scientist interviews, in the Machine Learning round.

Short answer

Elbow method, silhouette score, gap statistic, or domain knowledge.

How to answer it

There is no ground truth, so the honest answer combines a statistic with the question the clusters are for.

from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
scores = {k: silhouette_score(X, KMeans(k, n_init=10, random_state=0).fit_predict(X)) for k in range(2, 11)}

Then the constraint that usually decides it: what will be done with the clusters. A marketing team can act on four segments and not on eleven. If the silhouette says six and the business can use four, four with a note is the right answer.

Standardise features first, since k-means uses Euclidean distance and an unscaled column dominates; and remember k-means finds round, similar-sized blobs, so if the data is not shaped like that no k is right and the method is wrong.

Related questions

Practice this for real