Sub topic 7
Predictive segments: propensity and churn
This chapter assumes you have read the earlier chapters on customer data, consent, and RFM features. You should be comfortable with pandas, train/test splits, and the idea of a classifier.
What a predictive segment is
A clustering segment describes how customers look today. A predictive segment describes what they are likely to do next. You train a model on past customers whose outcome is known, then score current customers. The score becomes a segment, such as "top 20% likely to buy" or "high churn risk".
Two common targets:
- Propensity is the probability that a customer takes an action, such as buying in the next 30 days.
- Churn is the probability that a customer stops buying or cancels. You must define it first, for example "no purchase in the next 90 days".
The workflow
The key design choice is the cutoff date. Features come only from data before the cutoff. The label comes only from the period after it. If any post-cutoff data leaks into the features, the model looks excellent in testing and fails in production. This is called data leakage.
Worked example
This example trains a churn model on synthetic data and turns scores into three segments.
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
rng = np.random.default_rng(42)
n = 2000
df = pd.DataFrame({
"recency_days": rng.integers(1, 120, n),
"frequency": rng.integers(1, 20, n),
"monetary": rng.uniform(10, 800, n),
})
# Synthetic label: long recency and low frequency raise churn probability
logit = 0.04 * df["recency_days"] - 0.15 * df["frequency"] - 1.0
p = 1 / (1 + np.exp(-logit))
df["churned"] = rng.random(n) < p
X = df[["recency_days", "frequency", "monetary"]]
y = df["churned"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42, stratify=y
)
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)
scores = model.predict_proba(X_test)[:, 1]
print("AUC:", round(roc_auc_score(y_test, scores), 3))
result = X_test.copy()
result["churn_score"] = scores
result["segment"] = pd.qcut(
result["churn_score"], q=[0, 0.5, 0.8, 1.0],
labels=["low_risk", "watch", "high_risk"],
)
print(result.groupby("segment", observed=True)["churn_score"].agg(["count", "mean"]))AUC (area under the ROC curve) measures how well scores rank churners above non-churners. A value of 0.5 is random and 1.0 is perfect. The pd.qcut call splits customers by score rank, so the top 20% become "high_risk".
Choosing thresholds
You can cut scores by percentile or by probability value. Each has a cost.
| Method | Strength | Weakness |
|---|---|---|
| Percentile (top 20%) | Fixed audience size, easy to budget | Includes low-risk customers if scores are low overall |
| Probability (above 0.6) | Meaning stays stable over time | Audience size changes when the model drifts |
Pick based on the action. A costly offer suits a small, high-probability group. A cheap reminder can reach a larger one.
Common errors
- Leakage. Using a feature such as "cancellation ticket opened" that only exists once the customer has already decided to leave.
- Ignoring calibration. A score of 0.7 should mean about 70% of such customers act. If you use scores as probabilities, check this with a calibration plot.
- Random splits on time-based data. Test on a later period than you train on, because production always predicts forward.
- Targeting the wrong people. A high churn score does not mean the customer will respond to a retention offer. Some would have stayed anyway, and some are lost regardless. Measuring this needs the holdout designs covered later.
Exercise
Your churn model scores 0.97 AUC in testing but performs poorly after launch. One feature is "days since last support-cancellation request". What is the likely problem, and how do you fix it?
Show answer
The feature causes data leakage. A cancellation request is recorded only when the customer is already leaving, so it reveals the label. At scoring time it is not available early enough to be useful. Remove it, rebuild features using only data from before the cutoff date, and test on a later time period.