What is data leakage, and how do you prevent it?
Data leakage is when information unavailable at prediction time influences training. It produces excellent validation scores and a model that fails in production, and it is the single most common serious mistake in applied machine learning.
The main forms:
- Target leakage — a feature that is a consequence of the outcome. Including "number of collection calls" when predicting default, or "discharge medication" when predicting diagnosis. The giveaway is suspiciously high performance and one feature dominating importance.
- Train-test contamination — fitting a scaler, imputer, or encoder on the full dataset before splitting. The transformer has then seen the test data's distribution.
- Temporal leakage — training on data from after the prediction point. Random splits on time series always do this.
- Group leakage — the same entity appearing in both train and test, so the model memorises rather than generalises.
- Duplicate rows spread across splits.
How to prevent it:
- Split first, then fit everything on the training split only. Use a scikit-learn
Pipelineso preprocessing is fitted inside each cross-validation fold automatically — this eliminates the most common form structurally. - Split by time for temporal problems, and by group where entities repeat.
- Interrogate every feature: would this value exist, with this content, at the moment of prediction?
- Treat implausibly good results as a bug report, not a success.





