🏢 The situation
Max is preparing next year's strategy:
"Marketing splits customers into 'new' and 'old' — that's the stone age. I want to know what customer types we really have, to build different products and communications. There are no labels — nobody knows the 'correct' segments. Is this possible?"
Lena:
"This is unsupervised learning. There is no target and no 'accuracy': segmentation quality is measured by whether the business can use it. Build RFM features, cluster, and above all — describe the segments in words. Clusters without interpretation are garbage."
🎯 Your task
- Understand K-Means: how it works, how to pick the number of clusters.
- Prepare features for clustering (scaling is critical!).
- Interpret the segments and hand them to the business.
📚 Theory
K-Means in 4 steps
- Pick K random centers.
- Assign every point to the nearest center.
- Recompute the centers as the means of their points.
- Repeat 2–3 until convergence.
Properties: fast, simple; finds convex clusters of roughly equal size; sensitive to feature scale and outliers; the result depends on initialization (cured by n_init=10, k-means++).
How to choose K
- Elbow method: plot inertia (sum of squared distances to centers) vs K; look for the "bend".
- Silhouette score (−1…1): how much closer a point is to its own cluster than to the nearest other; higher is better.
- The business criterion (the main one): how many segments can the team actually serve? 4–7 is usually the ceiling for marketing.
Scaling is not optional
K-Means measures Euclidean distance. If "revenue" is in thousands and "purchase frequency" in single digits, clustering collapses to revenue. StandardScaler is mandatory; log-transform skewed money amounts.
RFM — the classic of customer analytics
- R (Recency) — days since the last purchase;
- F (Frequency) — number of purchases over the period;
- M (Monetary) — total spend.
Plus behavioral: share of discounted purchases, category diversity, average basket.
Alternatives to K-Means
- DBSCAN — arbitrary-shape clusters, finds outliers itself; needs no K but is sensitive to density parameters.
- Hierarchical — a dendrogram; you see the nesting of segments.
- Gaussian Mixture — "soft" probabilistic clusters.