36. K-Means Clustering in Python
36.1 Groups Without Labels
Why. Sometimes you have no label column. Marketing still asks: “Can we group Pune customers into a few behaviour buckets?” That is clustering — unsupervised learning (Chapter 29).
What. K-Means places K centres and assigns each row to the nearest centre. You choose K. There is no y to score with accuracy — you judge whether groups are useful for business.
How.
Frame an unsupervised question
- List numeric behaviour columns (orders_per_month, avg_basket_inr).
- Admit: no “correct group” column exists.
- Plan to name clusters after you see them (e.g. “light”, “regular”, “bulk”).
import pandas as pd
cust = pd.DataFrame({
"orders_per_month": [1, 2, 8, 9, 3, 12],
"avg_basket_inr": [200, 250, 900, 950, 300, 1200],
"city": ["Pune", "Nashik", "Pune", "Mumbai", "Kolhapur", "Pune"],
})
print(cust[["orders_per_month", "avg_basket_inr"]])
print("No churn label here — only groups.")
Ravindra Bagale's Tip
Clustering ≠ prediction of known y. Interview madhe supervised vs unsupervised clear theva.