Skip to content

How it's builtMachine learning

Customer Segments

k-means in your browser groups the customers of Thread & Loom, a clothing and home-textiles shop in Coimbatore that I made up, by how recently, how often and how much they buy. Then it names each group and suggests what to send it.

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

  1. 01
    Read

    Orders from the sample or your CSV; dates in either common format, and bad rows set aside with the reason.

  2. 02
    RFM

    Recency, frequency and spend for each customer.

  3. 03
    Scale

    Orders and spend on a log scale, then all three standardised.

  4. 04
    Cluster

    k-means++ with eight restarts for each k from 2 to 8.

  5. 05
    Choose

    Inertia and silhouette for every k; the best silhouette is the starting point.

  6. 06
    Name

    Each centre named by a written rule, with an action for the segment.

  7. 07
    Export

    Customer, segment and RFM as a CSV, ready for the email or messaging tool.

Results

Best k
5
of 2 to 8, by silhouette
Silhouette
0.636
−1 to 1; higher is cleaner
Agreement
98.8%
with the five behaviours I planted
Spread on the chart
95.2%
kept by the two PCA axes

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

PartWhat it does hereLicence
My machine-learning libraryk-means++, PCA, the silhouette score, the scaler and the sample generator, written in TypeScript and unit-testedMine
My CSV readerReads your orders file and writes the exports, quoted fields and allMine
PreactThe interactive demoMIT
AstroThe page, built as static HTMLMIT

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.