A subscription app trains a churn model on last quarter's customers. The data team exports one row per customer, every column the warehouse has, and the model scores a cross-validated AUC of 0.949. A month after launch, the retention team reports that the weekly list of at-risk customers is nearly empty. The model now catches 4% of the customers who leave. Its strongest feature was "received a retention call", and the retention team only calls customers who have already asked to cancel.
The common misconception
Many developers treat the historical table as the feature set: if a column is in the data and it predicts the label, it's fair game. It's an easy belief to hold:
The data is real. Every value was really recorded; nothing was invented or copied from the label column.
Validation approves it. Cross-validation and the test set both reward the column, so it looks like signal.
The rule everyone learns is about rows: keep the test rows out of training. Nobody says anything about columns.
The half-truth: every value in the table is true. But it's true as of the day you exported it, not as of the moment your model will be asked to decide.
What actually happens
A prediction happens at a point in time: when a customer opens the app, when a loan application arrives, when a patient is admitted. A feature is legitimate only if, at that moment, its value already exists and equals the value you trained on. Google's Machine Learning Crash Course puts it as a rule: during training, use only the features you'll have available when serving. Breaking it is , also called label leakage: information about the label reaching the model through a feature.
The model takes the shortcut
A column written because of the outcome is the easiest pattern in the data, so the model leans on it. The script rebuilds the churn story. Three honest features carry a weak signal; the fourth records a retention call, made for 80% of the customers who cancelled. The model trains on 4,000 customers, and the other 2,000 play live traffic. At prediction time nobody has cancelled yet, so for them the call column is 0 for everyone, as it would be in production.
import numpy as npfrom sklearn.ensemble import HistGradientBoostingClassifierfrom sklearn.metrics import recall_score, roc_auc_scorefrom sklearn.model_selection import cross_val_scorerng = np.random.default_rng(0)X = rng.normal(size=(6000, 3)) # known before anyone cancelsy = rng.random(6000) < 1 / (1 + np.exp(1.5 - X @ [-0.8, 0.5, 0.7]))# the retention call happens after the outcome: not a featuremodel = HistGradientBoostingClassifier(random_state=0)auc = cross_val_score(model, X[:4000], y[:4000], cv=5, scoring="roc_auc").mean()print(f"cross-validated AUC {auc:.3f}")proba = model.fit(X[:4000], y[:4000]).predict_proba(X[4000:])live_auc = roc_auc_score(y[4000:], proba[:, 1])live_recall = recall_score(y[4000:], proba[:, 1] >= 0.5)print(f"live AUC {live_auc:.3f}, recall {live_recall:.3f}")# cross-validated AUC 0.745# live AUC 0.738, recall 0.277
measures how well the model ranks churners above loyal customers: 0.5 is a random order, 1.0 is perfect. The leaky model reports 0.949. On live data it ranks at 0.715 and catches 4.3% of churners at the default cutoff, because it learned that "no call" means "probably staying", and in production nobody has had a call. The honest model reports a modest 0.745 and delivers 0.738, catching 27.7%. Its cross-validation predicted production; the leaky one's didn't. The leaky model even ranks worse, because it spent its effort on the shortcut instead of the real signals.
Why cross-validation can't see it
The leak lives in a column, so it's in every row of every fold: training and validation rows were exported the same way, after the outcome. Cross-validation measures how well the model generalizes to new rows from the same table. It can't tell you whether the table matches the world at prediction time. Kapoor and Narayanan list illegitimate features as their own category in a taxonomy of leakage, and note that judging legitimacy takes domain knowledge, so they ask researchers to justify every feature instead.
Three shapes of leakage
Consequences of the outcome. The retention call. Kapoor and Narayanan cite a study that used anti-hypertensive drug use to predict hypertension. The Crash Course describes a cancer model that leaned on hospital names, because some hospitals specialize in cancer; at inference the hospital wasn't known yet.
Values updated after the fact. "Days since last login", "account status" or "lifetime spend" read from today's tables for last year's rows. Rule #31 of Google's Rules of ML warns that a table you join at training and serving time may change in between.
Proxies of the label. In civil war prediction, one paper used several variables that were proxies for civil war itself and reached near-perfect accuracy. Corrected, no model beat a baseline that predicted the previous year's outcome.
Feature in the churn table
Known when you predict?
Verdict
Tenure in months
Yes
Use
Support tickets so far
Only if counted up to the prediction date
Use with a cutoff
Received a retention call
No, it follows a cancel request
Drop
Days since last login, computed at export
No, it keeps growing after the customer leaves
Recompute as of the prediction date
Account status
No, "closed" is the label
Drop
How to apply it in your project
Pin down the prediction momentWrite down exactly when the model is called and what the system knows at that instant.
Audit every columnFor each feature, record who writes it, when, and whether it can change after the prediction moment.
Build features as of a cutoffCompute each row's features from events before its prediction time, not from current tables.
Validate out of timeTrain on earlier periods and evaluate on later ones, with features rebuilt as of each prediction date.
Train on what serving seesRule #29 of the Rules of ML: log the features at serving time and use those logs for training.
Key takeaways
A feature is legitimate only if its value exists, unchanged, at the moment of prediction.
The leaky churn model scored 0.949 in cross-validation and caught 4.3% of churners live.
Cross-validation can't detect target leakage: the leak is in every fold.
Audit columns by when they're written, build features as of a cutoff, and validate on later data.
التعليقات (0)