Module 14 · Capstone Projects: Three Portfolio Pieces · Lesson 47 of 51
Capstone 2: Predict churn and plan retention
The marketing lead has a $1,500 budget for retention offers. Build and validate a churn model, pick who gets an offer using expected value, profile the at-risk members, and present a plan.
In this lesson · about 75 minutes
- Build, cross-validate and test a churn classifier in a pipeline
- Turn scores into a contact list using lift and expected value
- Profile at-risk members and recommend actions
Your plan
- Features: the member columns plus ratios from Module 10 (spend per visit, complaints per year).
- Model: logistic regression and gradient boosting in pipelines, compared with 5-fold cross-validated AUC; test once.
- Lift: of the top 10% / 20% / 30% by risk, what share really churned compared with the base rate?
- Expected value: an offer costs $5 and keeps a churner 40% of the time; a kept member is worth about $60. Contact members while probability × 0.4 × 60 > 5.
- Profile: how do high-risk members differ (visits, complaints, app use, branch)?
- Plan: who, how many, expected saving, and how to measure it (an A/B holdout!).
Model, cross-validation and lift table
Exercise
Lift in the top 20%
Using risk (test-set churn probabilities) and y_test, set top20_rate to the churn rate among the 20% of test members with the highest risk (use int(len(risk) * 0.2) members), base_rate to the overall test churn rate, and lift to top20_rate ÷ base_rate. Round all three to 3 decimals.
Code that runs before yours
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.linear_model import LogisticRegression
members = pd.read_csv("kc_members.csv")
members["spend_per_visit"] = members["avg_spend"] / members["visits_90d"].clip(lower=1)
members["complaints_per_year"] = members["complaints"] / (members["tenure_months"].clip(lower=1) / 12)
num = ["age", "tenure_months", "visits_90d", "avg_spend", "app_user", "complaints", "spend_per_visit", "complaints_per_year"]
cat = ["home_branch", "plan"]
X_train, X_test, y_train, y_test = train_test_split(members[num + cat], members["churned"], test_size=0.3, random_state=7, stratify=members["churned"])
prep = ColumnTransformer([("num", StandardScaler(), num), ("cat", OneHotEncoder(handle_unknown="ignore"), cat)])
clf = Pipeline([("prep", prep), ("model", LogisticRegression(max_iter=1000))]).fit(X_train, y_train)
risk = clf.predict_proba(X_test)[:, 1]Exercise
Who gets an offer?
Score all 800 members with clf. A member is worth contacting when probability × 0.4 × 60 > 5. Set n_worth to how many members pass that rule, n_send to the number of offers actually sent (at most 300, the budget), and expected_net to the expected value of contacting the top n_send members by risk: the sum of p × 0.4 × 60 − 5, rounded to 0 decimals.
Code that runs before yours
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.linear_model import LogisticRegression
members = pd.read_csv("kc_members.csv")
members["spend_per_visit"] = members["avg_spend"] / members["visits_90d"].clip(lower=1)
members["complaints_per_year"] = members["complaints"] / (members["tenure_months"].clip(lower=1) / 12)
num = ["age", "tenure_months", "visits_90d", "avg_spend", "app_user", "complaints", "spend_per_visit", "complaints_per_year"]
cat = ["home_branch", "plan"]
X_train, X_test, y_train, y_test = train_test_split(members[num + cat], members["churned"], test_size=0.3, random_state=7, stratify=members["churned"])
prep = ColumnTransformer([("num", StandardScaler(), num), ("cat", OneHotEncoder(handle_unknown="ignore"), cat)])
clf = Pipeline([("prep", prep), ("model", LogisticRegression(max_iter=1000))]).fit(X_train, y_train)
risk = clf.predict_proba(X_test)[:, 1]Profile the high-risk members
Write the retention plan in five bullet points.
Consider these questions and write down your thoughts. What you write stays on this device.
Exercises solved: 0 of 2
Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.