Project · Customer segmentation

Customer Segment Classifier

Customer segments learned from two years of real orders. This project walks through cleaning the transactions, turning them into features for each customer, finding segments with k-means, and checking whether the segments predict what customers did the following year. A classifier built from the result runs in your browser.

The data

776,577 real order lines from 5,852 customers

Every order placed with a UK-based online retailer of gifts and homeware between December 2009 and December 2011: 36,594 orders, £17.1M in sales, customers in 41 countries. Many customers are wholesalers buying for their own shops, so the data is closer to a business customer base than a typical web shop.

What each order line contains

  • Invoice: the order number; a leading C marks a cancellation.
  • StockCode and Description: the product.
  • Quantity and Price: units and unit price in pounds sterling.
  • InvoiceDate: date and time of the order.
  • Customer ID and Country: who ordered, and where they are.

There are no customer attributes such as size or industry, so segments have to come from buying behaviour alone.

What cleaning removed

1,067,371 rows delivered, 776,577 kept. Each rule runs in SQL and logs what it removed:

  • · Exact duplicates: 34,335 (counted twice otherwise)
  • · No customer id: 235,151 (can't be attributed to a customer)
  • · Not a product: 3,662 (postage, fees, discounts and manual lines aren't purchases)
  • · Cancellations: 17,586 (kept separately for the return rate, not counted as sales)
  • · Zero or negative quantity or price: 60 (stock adjustments and write-offs)
View the dataset at UCI ↗Daqing Chen, UCI Machine Learning Repository · CC BY 4.0A re-creation, on public data, of a customer stratification model built at Clarke Power in 2019.

Feature engineering

From orders to three numbers per customer

Pick a customer to see their orders become the inputs the model compares. Only orders before 1 December 2010 are used, so nothing from the future leaks in.

  1. Their orders in the 12 months before 1 Dec 2010

    DateProductsValue
    15 Feb 20106£897
    29 Apr 201014£579
    2 Jun 201021£197
    24 Jun 20104£373
    28 Jun 20107£263
    8 Sep 20109£316
    5 Oct 20108£177
    17 Nov 20107£107
    21 Nov 20105£233
    23 Nov 20104£408
    United Kingdom£3,549

    Latest order includes: 100 × jumbo bag owls, 20 × jumbo bag scandinavian paisley, 20 × jumbo bag pink vintage paisley.

  2. Summarised as three numbers

    Days since last order
    8 days
    Orders in the last 12 months
    10
    Spend in the last 12 months
    £3,549

    Recency, frequency and spend. Other features (returns, product range, trend) were tested and are used to describe segments, not to form them.

  3. Put on a common scale

    FeatureValueLogCappedScaled
    Recency (days)82.162.16-1.17
    Orders102.402.40+1.60
    Spend£3,5498.178.17+1.29
    • log: Log scale, so £100 → £1,000 counts as much as £1,000 → £10,000.
    • capped: Capped at the 1st and 99th percentile, so one huge account can't pull a segment.
    • scaled: Standardised: 0 is the typical customer, ±1 is one standard deviation.
  4. Compared with each segment's centre

    • Champions0.08
    • Slowing regulars2.11
    • Newcomers2.63
    • Gone quiet3.86

    Distance in scaled units; shorter is closer. This customer is nearest to Champions, then Slowing regulars.

Interactive demo

Classify a customer

k-means found 4 segments among the 4,239 customers active in the year before 1 December 2010. Move the sliders to place a customer of your own.

900 of 4,239 customers, 1 Dec 2010ChampionsSlowing regularsNewcomersGone quiet

Recent customers are at the top. Spend, the third input, isn't on this chart, which is why some colours overlap.

Describe a customer

Nearest segment

Slowing regulars

Several orders, but the last one was about two months ago

Distance 0.91; next closest is Newcomers at 1.01. A borderline customer.

Action: Check in before they lapse: reorder reminders, a call for the larger accounts.

Classified in your browser · no server call

  • Champions

    Order often, ordered recently, spend the most

    Customers
    18%
    Revenue
    65%
    Last order
    9 days ago
    Orders a year
    9
    Typical order
    £356
    Returned
    2.6%
  • Slowing regulars

    Several orders, but the last one was about two months ago

    Customers
    27%
    Revenue
    24%
    Last order
    61 days ago
    Orders a year
    4
    Typical order
    £355
    Returned
    2.5%
  • Newcomers

    First orders in the last few months

    Customers
    20%
    Revenue
    6%
    Last order
    14 days ago
    Orders a year
    2
    Typical order
    £255
    Returned
    1.7%
  • Gone quiet

    One small order, months ago

    Customers
    34%
    Revenue
    5%
    Last order
    169 days ago
    Orders a year
    1
    Typical order
    £205
    Returned
    2.5%

Shares and medians per segment. Returned is the average share of purchased value later cancelled.

Metrics

Model quality, segment value and what happened next

Model quality

Are the segments real, or an artefact of the method?

View the Python model ↗

k-means always returns segments, even from noise. These checks were run before choosing: several feature sets, 3 to 8 segments each, each re-fitted on resampled data.

4

Segments

Chosen from 3 to 8

0.34

Separation

Silhouette score, best for these inputs

0.93

Stability

Agreement over 20 resamples (1 = identical)

18%

Smallest segment

Share of customers, big enough to act on

Separation with 4 segments, by feature set

  • Recency, orders, spend (chosen)0.34
  • Recency, orders, typical order0.27
  • + product range0.29
  • + return rate0.26
  • Seven features0.29

Silhouette: how much closer customers are to their own segment than to the next one, from −1 to 1. Values from 0.26 to 0.50 are usually read as real but overlapping groups, which is what customers are.

Segment value

Who brings the revenue?

Revenue in the twelve months before 1 December 2010, by segment.

65%

of revenue from Champions

18% of customers

£7,085

Avg revenue, Champions

Per customer, in the year

34%

of customers are Gone quiet

5% of revenue

£8.3M

Revenue in the year

4,239 active customers

Share of customers and of revenue

CustomersRevenue
Champions18%65%
Slowing regulars27%24%
Newcomers20%6%
Gone quiet34%5%

Validation

Do the segments predict the next year?

Segments were set on 1 December 2010 and checked against what customers did in 2011. A plain RFM score cut into the same 4 groups is the baseline: it predicts spend slightly better, because a single score ranks customers by value well. The segments add the reason behind a middling score (new and growing, or regular and slipping away), which decides the action.

4%

of Champions stopped buying

vs. 61% of Gone quiet

£5,969

avg 2011 spend, Champions

vs. £293, Gone quiet

29%

of 2011 spend explained

By the segments (R², log spend)

31%

explained by RFM score

Baseline, same number of groups

What each segment did in 2011

Stopped buyingAverage spend
Champions4%£5,969
Slowing regulars27%£1,134
Newcomers36%£542
Gone quiet61%£293

Change over time

Where were customers a year later?

Each row is a segment on 1 December 2010; columns show the same customers on 1 December 2011, scored with the same model. “Lapsed” means no order in between.

53%

of Champions stayed

28% became Slowing regulars

27%

of Slowing regulars lapsed

The group a check-in is for

23%

of Newcomers became regulars

Champions or Slowing regulars

61%

of Gone quiet lapsed

Not worth heavy spend

2010 segment → 2011 segment

FromChampionsSlowing regularsNewcomersGone quietLapsedCustomers
Champions53%28%7%8%4%765
Slowing regulars12%29%9%24%27%1,161
Newcomers7%16%18%23%36%868
Gone quiet1%7%8%22%61%1,445

Data architecture

From a spreadsheet to a classifier in your browser

A script loads the workbook into Postgres, where SQL cleans it and builds one row of features per customer. A Python script finds the segments with scikit-learn, and an export script writes a small file the page reads.

Customer segments pipeline architectureGrouped into data source, data pipeline and serving. A TypeScript script loads the Online Retail II workbook from UCI into Supabase. SQL cleans it, logging the rows each rule removes, and builds 13 feature snapshots. A Python script runs k-means with scikit-learn and checks the result, then an export script writes a small JSON file. The static page on Vercel reads it, and the classifier runs in the browser.
  1. Data source
  2. UCI repository

    Online Retail II

    1.07M order lines

  3. Data pipeline· run on demand
  4. TypeScript script

    Load

    Read the workbook, land rows

  5. Supabase · SQL

    Clean

    Five rules, each one logged

    Supabase · SQL

    Cleaning log

    Rows removed, by rule

  6. Supabase · Postgres

    Features

    13 snapshots, no look-ahead

  7. Python · scikit-learn

    Segment

    k-means, stability, baseline

  8. JSON · 74 KB

    Export

    Profiles, results, centroids

  9. Serving
  10. Vercel · Next.js

    This page

    Static, no database calls

  11. Your browser

    Classifier

    Nearest segment centre

Historical data, run onceNo schedule: the orders end in December 2011. After cleaning, the raw rows are deleted to keep the database small; the cleaning log keeps the counts.

Load and clean

A script downloads the workbook from UCI and lands every row in Postgres. Five SQL rules remove duplicates, rows with no customer, non-product lines and adjustments, and move cancellations aside for the return rate.

Features and segments

SQL builds one row per customer for each snapshot date, using only earlier orders. A Python script (scikit-learn) compares feature sets and segment counts, checks stability and next-year results, and stores the chosen model.

Export and serve

The export writes profiles, results and the segment centres to a 74 KB file. The page is static; the classifier applies the same scaling as training and finds the nearest centre in your browser.