The problem
A shop that sends the same offer to every customer wastes it on people who would have bought anyway, and misses the ones drifting away. Segments let each group get the message that fits: thanks for the regulars, a nudge for the lapsing, a welcome for the new.
Recency, frequency and monetary value (RFM) are the three numbers that say most about a customer, and every till and shop platform already has them in its order history.
The data
1,200 customers and 7,176 orders over 18 months, May 2025 to October 2026, in store and online. Each order has a customer id, a date and an amount in rupees, which is all the model needs.
I generated them from five hidden behaviours: regular loyal buyers, big seasonal spenders who shop for the Aadi sale, Diwali and Pongal, recent first-timers, lapsing customers and one-off buyers. The model never sees which is which; it has to find the groups from the orders alone. The data is made up.
You can load your own CSV of orders instead, with customer_id, date and amount columns. It is read in your browser and never uploaded.
The model
RFM per customer: days since the last order, the number of orders and the total spend. Orders and spend go on a log scale, because a few big buyers would otherwise stretch everything, and all three are standardised so each counts equally.
k-means++ puts every customer with the nearest of k centres and moves the centres until nothing changes. The ++ seeding spreads the first centres out, and each k is run eight times from different starts, keeping the tightest result.
Choosing k: the elbow chart shows how much tighter the segments get with each extra one, and the silhouette scores how much closer each customer is to their own segment than to the next. The demo tries every k from 2 to 8 and starts at the best silhouette; you can choose another.
Names and actions come from each segment's centre: recent, frequent, high-spending groups are Champions; high spend with few orders, Big spenders; a long silence after a good history, At risk; and so on. The rule is a few lines of code with tests, so a marketer can read it and change it.
PCA squeezes the three measures into two axes for the chart, and its loadings say what each axis means. Here the two axes keep 95.2% of the spread.
The pipeline
- 01Read
Orders from the sample or your CSV; dates in either common format, and bad rows set aside with the reason.
- 02RFM
Recency, frequency and spend for each customer.
- 03Scale
Orders and spend on a log scale, then all three standardised.
- 04Cluster
k-means++ with eight restarts for each k from 2 to 8.
- 05Choose
Inertia and silhouette for every k; the best silhouette is the starting point.
- 06Name
Each centre named by a written rule, with an action for the segment.
- 07Export
Customer, segment and RFM as a CSV, ready for the email or messaging tool.
Results
Measured when this page was built, with the same code the demo runs. At k = 5 the segments are Champions (21.1%), Big spenders (14.8%), At risk (22.2%), New (16.2%), Hibernating (25.8%). Agreement counts the customers whose segment's most common hidden behaviour is their own; real data has no answer key, so the silhouette and the judgement of people who know the customers have to do.
For a client
With a real shop I would read the orders straight from the till or the e-commerce platform, refresh the segments every month and watch how customers move between them: a Champion turning At risk is worth a phone call.
I would add what RFM misses (what people buy, store or online, returns, how they answer campaigns) and check the names with the people who know the customers.
The export can feed the email or WhatsApp tool the shop already uses, so each segment gets its own message without anyone copying lists by hand.
Stack
| Part | What it does here | Licence |
|---|---|---|
| My machine-learning library | k-means++, PCA, the silhouette score, the scaler and the sample generator, written in TypeScript and unit-tested | Mine |
| My CSV reader | Reads your orders file and writes the exports, quoted fields and all | Mine |
| Preact | The interactive demo | MIT |
| Astro | The page, built as static HTML | MIT |
Privacy
Everything happens in your browser. A CSV you load is read on your device and never uploaded: there is no server behind this page.
Limits
- The sample is generated with clean groups, which flatters k-means. On a real shop's customers the segments blur into each other more.
- k-means finds compact groups in the scaled measures; an odd little group, such as a handful of wholesale buyers, gets absorbed into a bigger one.
- RFM sees only orders. Two customers with the same numbers can want very different things.
- The names come from a fixed rule. On another shop's data a segment can land on a name a person should check.