# Customer Segmentation and Clustering

> Grouping customers with rule-based RFM segments and unsupervised clustering such as k-means, then making segments actionable. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/data-analyst/clustering-segmentation
Last updated: 2026-11-28

Grouping customers with rule-based RFM segments and unsupervised clustering such as k-means, then making segments actionable.

Intermediate · about 6 hours · difficulty 3/5

Learn first: [Exploratory Data Analysis](https://www.internhack.in/roadmaps/tracks/data-analyst/exploratory-analysis)

Marketing and product teams want to treat different customers differently. Segmentation finds groups with similar behaviour so each can get a fitting message, offer or experience.

### Start with rules: RFM

**Recency** (days since last purchase), **Frequency** (number of purchases) and **Monetary** value (total spend). Score each 1-5 by quintile and combine: "555" customers are champions, high recency with low frequency are at risk. RFM is transparent and often enough.

### Clustering with k-means

1. Choose behavioural features (orders per month, average basket, share of discounted orders, tenure).
2. **Scale** them (standardise) so large-valued features do not dominate.
3. Run k-means for several values of k; use the **elbow method** or **silhouette score** as a guide, but prefer the k that gives interpretable groups.
4. **Profile** each cluster: average feature values, size, revenue share.
5. Name clusters in business language ("bargain hunters", "loyal big-basket").

```
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
X = StandardScaler().fit_transform(df[features])
df["segment"] = KMeans(n_clusters=4, n_init=10, random_state=0).fit_predict(X)
```

### Making segments useful

A segment only matters if it leads to a different action. Check stability (do customers stay in their segment next month?), size (too small to act on?) and whether segments differ in outcomes you care about.

**Pitfall:** clustering on unscaled or highly skewed features and getting one giant cluster plus outliers. Log-transform skewed spend first. You are ready to move on when you can build RFM segments and a k-means segmentation on the same data and explain which is more useful and why.

## Key concepts

- **RFM segmentation**
- **Feature selection for segments**
- **Scaling and transforming features**
- **k-means clustering**
- **Elbow and silhouette methods**
- **Profiling and naming clusters**
- **Hierarchical clustering and DBSCAN**

## Mini project

Using the UCI Online Retail dataset, build RFM scores and a k-means segmentation with 3-6 clusters, profile each segment, and propose one marketing action per segment.

## Check yourself

Why must features be scaled before k-means, and how do you choose the number of clusters?

## Free resources

- [scikit-learn: clustering](https://scikit-learn.org/stable/modules/clustering.html) (scikit-learn)
- [RFM (market research)](https://en.wikipedia.org/wiki/RFM_%28market_research%29) (Wikipedia)

Previous: [Time Series Forecasting](https://www.internhack.in/roadmaps/tracks/data-analyst/forecasting) · Next: [Predictive Modelling with scikit-learn](https://www.internhack.in/roadmaps/tracks/data-analyst/predictive-modelling)

## More in Machine Learning for Analysts

- [Time Series Forecasting](https://www.internhack.in/roadmaps/tracks/data-analyst/forecasting)
- [Customer Segmentation and Clustering](https://www.internhack.in/roadmaps/tracks/data-analyst/clustering-segmentation)
- [Predictive Modelling with scikit-learn](https://www.internhack.in/roadmaps/tracks/data-analyst/predictive-modelling)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Data Analyst Roadmap](https://www.internhack.in/roadmaps/tracks/data-analyst)
