Ravindra BagaleCourses & study guides मराठी Track your progress

36. K-Means Clustering in Python

36.3 Choose K Simply

Why. K is a business + data choice. Too small = mushy groups. Too large = every customer is their own island.

What. Beginners often try K = 3, 4, 5 and compare: are groups interpretable? A full “elbow plot” can come later; start with readable K.

How.

Choose K simply

  1. Ask ops: “How many playbooks can we run?” (often 3–5).
  2. Fit KMeans for those K values.
  3. Inspect cluster sizes and mean feature values.
  4. Pick the K you can explain in one slide.
import pandas as pd
from sklearn.cluster import KMeans

X = pd.DataFrame({
 "orders_per_month": [1, 2, 2, 8, 9, 10, 3, 12, 11],
 "avg_basket_inr": [180, 220, 260, 880, 920, 1000, 300, 1150, 980],
})
for k in (2, 3, 4):
 km = KMeans(n_clusters=k, n_init=10, random_state=42)
 labels = km.fit_predict(X)
 print("K=", k, "sizes=", pd.Series(labels).value_counts().to_dict())

What you should see. Different size mixes as K changes. Prefer a K where each group has a clear story, not the fanciest number.