Skip to content

Team 3 · Programming for Business Analytics

Know your best customers.

RFM segmentation and K-means clustering for a UK online gift retailer that sells mainly to wholesalers. One marketing budget, two jobs: reward the customers who drive revenue and win back the ones slipping away.

Runs on Online Retail II from UCI. Your file is processed in the browser.

The segment map

Every customer gets a Recency and a Frequency score from 1 to 5. The cell they land in decides their segment.
ChampionsLoyalty rewardsLoyalLoyalty rewardsPromisingNurtureNeed attentionNurtureAt riskWin-back emailsLostMinimal spend5432112345Frequency score (F) · 5 = most frequentRecency score (R) · 5 = most recent
  • Loyalty rewardsChampions, Loyal
  • NurturePromising, Need attention
  • Win-back emailsAt risk
  • Minimal spendLost
invoice lines in the raw data
~1M
December 2009 to December 2011
2 years
business questions answered
5
segments, one budget decision each
6

The brief

One budget. Who gets the loyalty reward, and who gets the win-back email?

We work for a UK online gift retailer that sells mainly to wholesalers. The Marketing Manager has one budget for loyalty rewards and win-back emails, and needs to know who deserves which.

  • The client

    The Marketing Manager of a UK online gift retailer whose buyers are mostly wholesalers and repeat business customers.

  • The decision

    How to split a single budget between loyalty rewards for the best customers and win-back emails for the ones drifting away.

  • Success measure

    Every pound lands on the segment with the most revenue at stake: more repeat orders from the best customers and more lapsed customers buying again.

The data

Online Retail II

About 1 million invoice lines from December 2009 to December 2011, published in the UCI Machine Learning Repository. Each line is one product on one invoice.

Columns

  • Invoice
  • StockCode
  • Description
  • Quantity
  • InvoiceDate
  • Price
  • Customer ID
  • Country
View the dataset on UCI

Watch-outs in the raw file

  • Cancelled invoices start with “C”
  • Some quantities are negative
  • Many rows have no customer ID
  • Postage and fees sit among the products

Business questions

Five questions, answered with data

Each section of the explorer answers one of them, in the same order as the notebook.

  1. Q1

    How do monthly sales change through the year?

    Revenue resampled by month to find the peak season, with a 3-month rolling average to show the trend.

    resample
  2. Q2

    Which countries and products bring the most revenue?

    Revenue ranked by country and by product, with the share each one contributes.

    groupby
  3. Q3

    Who are the Champions, and who is at risk?

    Recency, Frequency and Monetary value for every customer, scored from 1 to 5 by our own function and mapped to six segments.

    def rfm_segment()
  4. Q4

    What share of revenue comes from the top 20% of customers?

    Customers ranked by spend to see how concentrated revenue is.

    cumsum
  5. Q5

    Do new customers come back?

    Customers grouped by the month of their first purchase, and the share who buy again 1, 2 and 3 months later, shown as a heatmap.

    pivot_table
  6. Customer Segment Explorer

    See it on the real data

    Open the explorer

Method

From a million rows to a marketing plan

One step beyond descriptive statistics: rule-based RFM segments, checked against K-means clustering.

  1. 01

    Clean

    Seven logged steps remove guest rows, cancellations, zero or negative quantities and prices, postage and fee codes, unreadable dates and exact duplicates.

  2. 02

    Score

    Recency is days since the last order, Frequency the number of invoices and Monetary the total spend. Each is split into five equal groups and scored 1 to 5.

  3. 03

    Segment

    Our function maps the Recency and Frequency scores to six segments, from Champions to Lost.

  4. 04

    Cluster

    K-means runs on log-transformed, standardised R, F and M values. The elbow method picks the number of clusters.

  5. 05

    Compare

    A cross-tab of clusters against segments shows where the rules and the algorithm agree, and where they do not.

  6. 06

    Recommend

    Each recommendation is tied to a number from the analysis and an estimated revenue impact.

Segment rules

R = 5 means the most recent buyers, F = 5 the most frequent ones.

SegmentRuleBudget
ChampionsR 4–5 and F 4–5Loyalty rewards
LoyalR 3–5 and F 3–5, not ChampionsLoyalty rewards
PromisingR 4–5 and F 1–2Nurture
Need attentionR 3 and F 1–2Nurture
At riskR 1–2 and F 3–5Win-back emails
LostR 1–2 and F 1–2Minimal spend

K-means setup

  • Features: Recency, Frequency and Monetary value per customer
  • log1p transform to tame the long right tails
  • StandardScaler so no feature dominates the distance
  • k from 1 to 8 tested, the elbow picks the number of clusters
  • Clusters compared with the rule-based segments in a cross-tab

Segment playbook

Six segments, six budget decisions

What each segment means for the Marketing Manager's budget.

Champions

Loyalty rewards

R 4–5 and F 4–5

Who they are
Bought recently, buy often and spend the most.
What to do
VIP rewards: early access to new ranges, volume pricing and a named account contact.

Loyal

Loyalty rewards

R 3–5 and F 3–5, not Champions

Who they are
Order regularly and know the range well.
What to do
Tiered discounts that nudge them toward Champion level, plus cross-selling of best sellers.

Promising

Nurture

R 4–5 and F 1–2

Who they are
Recent buyers with only one or two orders so far.
What to do
A welcome series and a second-order incentive within 30 days.

Need attention

Nurture

R 3 and F 1–2

Who they are
Average recency and few orders: their interest is fading.
What to do
Seasonal reminders before the autumn peak, with a limited-time offer.

At risk

Win-back emails

R 1–2 and F 3–5

Who they are
Used to order often, but not for a long time.
What to do
Personal win-back emails with a reason to return. First in line for win-back spend.

Lost

Minimal spend

R 1–2 and F 1–2

Who they are
Ordered rarely, and long ago.
What to do
One low-cost automated email at most. No paid rewards.

Customer Segment Explorer

See it on the real data

Upload the Online Retail II file as CSV. The explorer cleans it, scores every customer, runs K-means and draws the charts. Filter by segment, country and date range, then download the result.

The file never leaves your browser.

  1. 1

    Upload the CSV

    Up to a million rows, read in chunks.

  2. 2

    Clean and score

    Seven cleaning steps, RFM scores, segments and clusters.

  3. 3

    Explore and export

    Linked charts, a cohort heatmap and a CSV download.