Classical machine learning
Data leakage
Leakage is when a model learns from information it would not have at prediction time. Offline scores look excellent, then production falls apart. Here is how to find it.
Leakage means the model can see something at training time that it will not have when it makes a real prediction. It is the most common way a strong offline score turns into a production failure. The model is not learning the task. It is learning a shortcut that only exists in the training data.
The main kinds
| Kind | What happens | Symptom |
|---|---|---|
| Target leakage | A feature is a proxy for the label, such as cancellation_reason used to predict churn | AUC is unrealistically high, and one column dominates the importance list |
| Train-test contamination | The same users or near-duplicate rows appear in both sets | The test score looks like the training score |
| Temporal leakage | A feature uses information from after the prediction time | Works in the notebook, fails on the first day of deployment |
| Preprocessing leakage | A scaler, imputer or encoder is fitted on all the data, including the test set | Mild optimism, and it fails audits |
| Group leakage | Repeated measurements are split at random | The model memorizes the identity of each entity |
Warning
If one feature alone gets an AUC above 0.95 on a hard problem, assume leakage until you prove otherwise.
Why churn with a cancellation reason fails
Suppose you predict whether a customer will churn, and one column is cancellation_reason. That column is filled in only after the customer cancels. The model learns "if the reason is filled in, the customer churned." The offline AUC is excellent. At prediction time, the column is empty for everyone, so the model has nothing useful.
The fix is to ask one question of every column: would this value exist at the moment the prediction is made? If not, remove it or move it to the label side.
A detective workflow
When a metric looks too good, work through these steps in order:
- Train a model on each feature alone. Any feature with an AUC far above the baseline is a suspect.
- Check timestamps. Compare the feature's time with the label's time.
- Search for columns that are functions of the label, such as
status,closed_atorfraud_tag. - Re-split by group or by time. If the metric collapses, the original split was contaminated.
- Compare the offline lift with a shadow deployment. If the online result is flat, suspect skew or a proxy label.
Common mistakes
- Fitting preprocessing on all the data. Fit scalers, imputers and encoders on the training split only, inside the pipeline.
- Splitting repeated users at random. Split by user, so no user appears on both sides.
- Shuffling time series. Split by time, so the model is always tested on the future.
Check yourself
- Why is
cancellation_reasonleakage for predicting churn? It is recorded because the customer cancelled, so it would not exist when the prediction is made. - Why does a random split leak for repeated users? The same user appears in both sets, so the model can memorize that user's pattern.
Key takeaways
- Leakage is information the model would not have at prediction time.
- A suspiciously strong feature or score is a signal to investigate, not to celebrate.
- Fit every preprocessing step on the training split only, and split by group or time when rows are related.
- Confirm offline wins with a shadow deployment or an experiment before you ship.
Further learning
Many leakage bugs start in feature code. The feature engineering page covers point-in-time features and how to keep them correct.
Related
Evaluating models
Choose metrics that match the decision, set thresholds from costs, check calibration, and guard against leakage. A high offline score means little until it holds up on real decisions.
Features and data preparation
Turning raw data into model inputs without leaking the future. Covers scaling, encoding, missing values, time windows, target encoding and class imbalance, and keeping training and serving identical.
ML in production
Feature stores, training-serving skew, monitoring for drift and the lifecycle of a deployed model.