Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 39 ยท Projects and Practice

Project: Customer Segmentation

Build a customer segmentation end to end: simulate transactions, engineer RFM features, scale, choose k, run k-means, profile each segment, validate stability and turn clusters into actions.

In this project you act as the data miner for an online shop. The marketing team wants to know which kinds of customers exist so it can send different offers to each group. There are no labels, so this is a clustering task. You will follow the KDD process: understand the goal, prepare data, model, evaluate and describe the result in business terms.

The data is simulated, so the project runs anywhere and the answer is reproducible. The steps are exactly what you would do with a real transactions export. Each code block below is self-contained, so the small generator function is repeated.

Step 1: Transactions and RFM features

Each transaction row has a customer, a day (0 to 364) and an amount. The generator hides four customer types: loyal big spenders, regular mid-range buyers, bargain hunters who buy often but little, and lapsed customers who bought long ago. We then compute the marketing trio:

  • Recency: days since the last purchase (small means engaged).
  • Frequency: number of purchases.
  • Monetary: total spending.
Python Simulate transactions and compute RFM

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Frequency and monetary are right-skewed: a few customers spend a lot, and they would dominate Euclidean distance. Two standard fixes are a log transform to reduce skew and standardization so all three features weigh equally (see data transformation).

Step 2: Choose k and cluster

Python Scale, choose k and run k-means

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look for the bend in inertia and the peak in silhouette, but let business sense decide too. Here the silhouette is highest at k = 3, yet it is almost as good for 4 to 7 (two of the segments are close in RFM space), and marketing can run three or four campaigns, not twelve. We continue with k = 4, which also separates lapsed customers from the rest.

Step 3: Profile the segments

A cluster number means nothing to marketing. Describe each cluster by the average original (not log) RFM values, its size and its share of revenue, then give it a name.

Python Profile and name the segments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the table like a marketer: a small segment with low recency, high frequency and high spend is the champions group; one with high recency (not seen for months) and low frequency is at risk or lapsed; many purchases with a small average is the bargain hunters. In this simulation the crosstab shows the clusters recover the hidden types almost exactly, which real data seldom does, so interpret real clusters more cautiously.

Step 4: Validate

Before presenting results, check that the segments are not an accident of this sample (evaluating clusters): recluster on random 80% subsets and compare the labels on shared customers.

Python Stability check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A stability near 1 means the segmentation would come out the same on a different sample of customers.

Step 5: Turn clusters into actions

Segment Typical behavior Action
Champions Recent, frequent, high spend Early access, loyalty rewards, ask for reviews
Regulars Steady mid-range buyers Cross-sell, bundles
Bargain hunters Frequent, small baskets Free-shipping threshold, volume discounts
Lapsed Not seen for months Win-back email with a time-limited offer

Finally measure the campaigns with a control group; segmentation only earns its keep if the targeted offers beat the generic one.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What do the letters in RFM stand for?

    Recency (days since last purchase), Frequency (number of purchases) and Monetary (total spend).
  2. 2. Why apply a log transform to frequency and monetary before k-means?

    Right-skewed features stretch distances; logging them makes the feature distribution more even.
  3. 3. How do you make cluster numbers useful to a business team?

    Descriptive names and metrics turn anonymous clusters into segments people can act on.
  4. 4. What does a stability ARI near 1 across resamples indicate?

    Similar clusters from different subsamples suggest a genuine structure rather than a sampling accident.

Summary

  • Build RFM features from raw transactions, log-transform and standardize them.
  • Choose k with the elbow, silhouette and business needs; run k-means.
  • Profile and name each segment, validate stability, then connect each segment to an action and test it.

Next: Project: Fraud Detection