Customer segmentation (K-means)
Group customers by buying behaviour (RFM) with K-means and turn clusters into marketing actions.
Intermediateâą 6â10 hoursđĒ 7 stepsPythonscikit-learnpandas
What you'll learn
- âUnsupervised learning
- âRFM features
- âChoosing k (elbow, silhouette) and describing clusters
What you'll build
- âRFM table from transactions
- âScaled features and K-means
- âElbow and silhouette plots
- âNamed segments with suggested actions
Step-by-step guide
- 1
Data
Use a transactions dataset such as the UCI Online Retail dataset.
- 2
Clean
Remove cancelled invoices and missing customer IDs; compute line revenue.
- 3
RFM
Recency (days since last order), Frequency (orders), Monetary (total spend) per customer.
- 4
Scale
Log-transform skewed values, then StandardScaler.
- 5
Pick k
Fit K-means for k = 2..10; plot inertia (elbow) and silhouette_score; choose a k you can explain.
- 6
Describe clusters
Average R, F, M per cluster; name them ("Champions", "At risk", "New").
- 7
Actions
One recommendation per segment, plus a 2D PCA plot coloured by cluster.
đ Make it stand out
- +Compare with DBSCAN or hierarchical clustering
- +Customer lifetime value
- +Dashboard for marketing