Contents/ Part XXI Β· ML Case Study/ Chapter 131

Case Study: Customer Lifetime Value

How much is each customer worth over the next year? This is a machine-learning regression project end to end: build a leakage-safe pipeline, beat a baseline with the right error metrics, read the residuals, and explain every prediction with SHAP, so marketing can spend its budget where it pays.

⏱️ ~22 min read
🐍 Full notebook included
πŸ“Š Chapter 131

When the thing you want to predict is a continuous dollar amount, the task is regression, not classification. Customer lifetime value (CLV) is the classic example: predict the revenue a customer will generate over the next 12 months from what you already know about them, then use those predictions to rank customers and target retention where it earns the most. A good model does two jobs at once, it ranks well, and its explanations reveal what drives value.

πŸ€–
Customer lifetime value is a regression target: a continuous dollar amount per customer. We judge a model not by accuracy (a classification word) but by how many dollars its predictions are off, MAE and RMSE, measured against a simple baseline and explained with SHAP.
🎯
What this case study ties together

It is the regression counterpart to the classification and clustering case studies of the ML Case Study: a ColumnTransformer pipeline, a gradient-boosted-trees model, honest error metrics against a baseline, residual diagnostics for a skewed target, and SHAP to turn the model into an explanation the business can act on.

1

The Business Question, and the Data (Steps 1–2)

Marketing has a fixed retention budget and wants to spend it on the customers most worth keeping. So the question is a regression one: for each of 1,500 customers, predict their next-year value from their behavior and account. The target, lifetime_value, is a dollar amount; the features are things known today, order frequency and size, recency, tenure, support load, subscription status, and acquisition channel.

πŸ“‚ Dataset Β· case-study-customer-lifetime-value--customer_value.xlsx

One row per customer: tenure_months, avg_order_value, orders_per_month, recency_days, product_categories, support_tickets, is_subscriber, and acquisition_channel (organic, paid search, social, referral, email), with the target lifetime_value.

2

Look Before You Model (Step 3)

Lifetime value is strongly right-skewed: the mean is about 1,612 dollars but the median is only 1,194 (skew ≈ 2.8). Most customers are modest, and a few whales are worth ten times the median. That shape drives two decisions: evaluate with RMSE and MAE rather than a single average, and reach for a tree-based model, which handles skew and non-linearity without a transform.

Lifetime value is right-skewed: many modest customers, a few whales median 1,194 mean 1,612 the long tail of whales pulls the mean above the median customer lifetime value (dollars) →
3

Build the Pipeline, and Beat the Baseline (Steps 4–7)

4–5

A leakage-safe pipeline, and a bar to clear

A ColumnTransformer passes the numeric features straight through and one-hot encodes the single categorical column, all inside a Pipeline with the model, so no preprocessing ever sees the test set. Then we set a baseline: predict every customer as the training-set mean. That naive rule is off by about 977 dollars on average. Any real model has to beat it to earn its complexity.

6–7

Gradient-boosted trees, cross-validated, then scored on held-out customers

A HistGradientBoostingRegressor, scikit-learn's fast cousin of XGBoost and LightGBM, cross-validates to an R-squared around 0.83 on the training set, so the signal is real. On the 25% of customers it never saw, it explains about 86% of the variance (R-squared 0.857) with an average error of just 298 dollars, less than a third of the baseline, cutting the average error by 69%.

Average error (MAE): the model beats the baseline by 69% $977 Baseline predict the mean $298 Boosted trees HistGradientBoosting MAE $0
A scatter of predicted versus actual lifetime value for held-out customers, clustered tightly along the diagonal for ordinary customers and looser for the rare high-value whales
From the notebook · Step 7
Predicted versus actual on customers the model never saw. Points hug the diagonal, tightest for the many ordinary customers and looser for the rare whales, exactly the footprint of a skewed target. R-squared 0.86, MAE 298 dollars.
4

Diagnose the Residuals, and Explain the Drivers (Steps 8–9)

A good score can still hide a biased model, so we check the residuals. They are centered near zero (no systematic over- or under-prediction), but their spread widens for high predictions: the model is dollar-precise on ordinary customers and fuzzier on whales, the classic footprint of a skewed target. That is fine for ranking; if we needed a precise figure per whale, we would model log value or quote a prediction interval (both explored in the Take It Further notebook).

Then the payoff: SHAP attributes each prediction to its features and, averaged, ranks what moves value most. Order frequency and average order value dominate, together they are the spend rate, followed by recency and tenure (loyalty), then subscription and the referral channel. Because SHAP works per customer, you can show exactly why the model values any individual.

A horizontal bar chart of SHAP importance: orders per month and average order value have by far the largest mean impact on the prediction, followed by recency, tenure, subscription, and the referral channel
From the notebook · Step 9
SHAP importance: the mean dollar impact of each feature on the prediction. How often and how much customers order dominate; recency and tenure follow. This is not just a leaderboard, SHAP explains each customer individually, which is what makes a prediction trustworthy.
5

From Prediction to Decision, and the Memo (Steps 10–12)

The point of the model is an action. Ranking the held-out customers by predicted value and taking the top 10% captures a large share of the actual value: those customers average about 4,402 dollars against an overall 1,525. A retention budget aimed there, rather than sprayed evenly, is where the model pays for itself, and because the ranking is SHAP-explainable, each offer can be tailored to that customer's value drivers.

Memo to the marketing team

We built a model that predicts each customer's next-12-month value from their behavior. On customers it had never seen, it is accurate to within about 298 dollars on average, roughly a third of the error of guessing the average, and it explains about 86% of the variation in value.

What drives value

The biggest levers are how often and how much customers order, followed by how recently they bought and how long they have been with us. Subscribers and referred customers are worth more, but that describes who is valuable, not a lever to pull, correlation is not causation.

The recommendation

Focus retention spend on the top decile by predicted value, who hold a large share of total value, with offers tailored to each customer's drivers. Re-check the model as new data arrives, since it assumes behavior stays stable.

One caution

Trust the ranking more than the exact dollar figure for whales, where the model is least precise. If a per-customer number is needed there, use the log-value model or a prediction interval.

🐍

Run the whole project in Python

The companion notebook is the full 12-step regression workflow: it frames the CLV question, explores the skewed target, builds the leakage-safe ColumnTransformer pipeline, sets a mean baseline, cross-validates and fits a HistGradientBoostingRegressor, scores it with RMSE, MAE, and R-squared against the baseline, diagnoses the residuals, interprets the drivers with SHAP, and turns the ranking into a targeting decision, all library-first with scikit-learn and shap.

πŸ““ View Notebook (code & outputs) β–Ά Open in Colab ⬇ View / Download on GitHub

View opens the rendered notebook instantly. Open in Colab runs it live. To run locally, install numpy, pandas, matplotlib, scikit-learn, shap, and openpyxl.

πŸŽ“ Key Takeaways

  • βœ“A dollar target is regression: judge it by MAE and RMSE against a baseline, not by "accuracy".
  • βœ“Always beat a baseline: predicting the mean was off by 977 dollars; the model cut that to 298, a 69% reduction.
  • βœ“One leakage-safe pipeline: the ColumnTransformer and model together mean preprocessing is fit only on training data.
  • βœ“Diagnose the residuals: near-zero bias but wider spread on whales, the honest footprint of a skewed target.
  • βœ“Explain, then act: SHAP names the drivers (frequency, order value, recency, tenure) and the ranking targets the top decile, where value concentrates.
6

Take It Further

Five ways to push the model further in the companion notebook:

1

Tame the skew with a log target

Model log-value, convert back with expm1, and see whether the errors get more even.

Hint: fit on np.log1p(y), compare MAE on whales versus the crowd.
2

A fair model leaderboard

Drop linear regression and a random forest into the same pipeline and compare by cross-validated R-squared.

Hint: hold preprocessing and the split fixed, change only the estimator.
3

Prediction intervals, not just points

Use quantile regression to give each customer a 5–95% range, then check its coverage.

Hint: HistGradientBoostingRegressor(loss="quantile", quantile=...).
4

Explain one customer

Decompose a single high-value prediction into its SHAP contributions that sum to the prediction.

Hint: shap_values for one row, plot the per-feature push.
5

From prediction to profit

Simulate a retention campaign and measure the extra profit the model's ranking earns over targeting at random.

Hint: assume a per-offer cost and a percentage lift, compare top-decile versus random.
πŸ““

All five, worked in a companion notebook

A second notebook, Take It Further, rebuilds this chapter's model and works every extension with visuals and explanations: a log-target comparison, a three-model leaderboard, quantile prediction intervals with a coverage check, a per-customer SHAP explanation, and a targeting-ROI simulation.

πŸ““ View Notebook (code & outputs) β–Ά Open in Colab ⬇ View / Download on GitHub
7

Quiz: Test Yourself

Eight questions on the regression workflow, from framing and baselines to residuals and SHAP. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved.

➑️
Up next

We predicted a value for each customer. Next we follow a quantity through time. Components of a Time Series opens Time Series & Forecasting, decomposing a series into trend, season, and noise before we ever forecast it.