You train a model. Cross-validation says 97%. You deploy it, and it performs barely better than guessing.
The most common reason is data leakage: information that wouldn't be available at prediction time found its way into training or validation. The model learned to cheat, and your evaluation let it.
Why leakage is so dangerous
Leakage doesn't cause errors. It causes good numbers. Nothing crashes, nothing warns you, and the better your score looks, the more confident you become in a model that won't work.
The five common ways it gets in
1. Preprocessing on all the data before splitting
Scaling, imputing missing values or selecting features using the whole dataset, then splitting, lets the test rows influence the training. The model has seen a summary of the data it's tested on.
Fix: split first. Fit every preprocessing step on the training data only, and apply it to the validation and test data.
2. Features that encode the answer
A churn model that uses "number of retention calls" or "refund issued" is often using consequences of churning to predict churning. In production, you won't know those values in time.
Fix: for every feature, ask "would I know this value at the moment I need the prediction?" If not, drop it.
3. The same entity on both sides of the split
If one patient, customer or device appears in many rows, a random split puts the same entity in both training and validation. The model recognises the individual instead of learning the pattern, and the score looks far better than it will on someone new.
Fix: split by group (all of an entity's rows go to one side), using grouped cross-validation.
4. Time travelling
For anything that changes over time (prices, demand, behaviour), a random split lets the model train on the future and predict the past.
Fix: split by time. Train on the past, validate on what comes after. For forecasting, use walk-forward validation.
5. Tuning on the test set
Every time you look at your test score and change something, a little of the test set leaks into your decisions. Do it enough and your "held-out" score is just another training score.
Fix: tune with cross-validation on the training data. Touch the test set once, at the end.
The habit that prevents most of it: pipelines
Put every preprocessing step and the model into a single pipeline, and cross-validate the whole pipeline. Then each fold fits its own scaler, imputer and encoder on its own training rows, and leakage from preprocessing becomes impossible by construction.
In scikit-learn that's Pipeline (or make_pipeline), passed to cross_val_score or GridSearchCV.
A quick leakage audit
Before you trust a score:
- Was every preprocessing step fitted inside the cross-validation folds?
- Would every feature be known at prediction time?
- Can the same entity appear in both training and validation?
- Does the split respect time, if time matters?
- How many times have I looked at the test set?
If a score looks too good to be true, assume leakage until you've ruled it out.
Preparing real data, including leakage and pipelines, is Stage 9 of SchoolWhool's Foundations of AI course.