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 does not predict a known answer (y), because the data has no labels. It only finds groups of similar rows. In interviews, clearly explain the difference between supervised learning (with labels) and unsupervised learning (without labels).
Ravindra Bagale's Tip – मराठी
Clustering आधीपासून माहित असलेलं उत्तर (y) predict करत नाही, कारण data ला labels नसतात. ते फक्त एकसारख्या rows चे गट शोधतं. Interview मध्ये supervised learning (labels सह) आणि unsupervised learning (labels शिवाय) यातला फरक स्पष्टपणे सांगा.
Ravindra Bagale's Tip – हिंदी
Clustering पहले से पता जवाब (y) predict नहीं करता, क्योंकि data में labels नहीं होते. यह सिर्फ एक जैसी rows के समूह ढूँढता है. Interview में supervised learning (labels के साथ) और unsupervised learning (labels के बिना) का फर्क साफ़-साफ़ बताओ.