Read first

Data leakage is when a model learns from information it will not have when it is used for real. The offline evaluation looks excellent; in production the model is much worse. It is the most common reason an ML result does not hold up.

How it works

The held-out score is only honest if the held-out rows had no influence on training, and if every feature is something that would exist at the moment of a real prediction. Leakage breaks one of those two conditions.

KindWhat happensFix
Preprocessing leakageScaling, imputing or feature selection fitted on all rows, test rows includedFit on training rows only; use a Pipeline
Target leakageA feature is a consequence of the label (“refund issued” when predicting fraud)For each column, ask: would this exist at prediction time?
Temporal leakageRandom split on time-ordered data, so the model trains on the futureSplit by time
Group leakageRows from the same person or object in both training and testSplit by group
Test-set tuningRepeatedly checking the test score and adjustingDecide on validation; touch test once

Example

Lesson 3 builds a dataset where the labels are coin flips and all 2,000 features are random noise. Selecting the 10 “best” features using every row, then testing on 20 held-out rows, gives 85% accuracy. Selecting them from the training rows only gives 40%, which is what pure guessing on 20 rows can look like. Nothing in the data was real; the 85% came entirely from letting the test labels choose the features.

Traps

  • One feature dominates the importance chart. That is often a leak, not a breakthrough. Look at what it records and when it is filled in.
  • IDs and timestamps can leak. A patient ID or a row number can correlate with the label if data was collected or sorted by outcome.
  • The serving pipeline computes features differently. If training used a field cleaned after the fact, and production sees the raw value, it is the same problem from the other side.

In the wild

Kapoor and Narayanan found leakage behind many machine-learning results in published science that did not reproduce (Patterns, 2023). In their case study on civil war prediction, the claim that complex models beat logistic regression did not survive once the leakage was fixed.

Leakage in data mining (Kaufman et al., 2012) is the classic paper on it. Cross-validation needs the same care inside every fold. Lesson: #3 · Overfitting and the bias-variance trade-off.

Check yourself

Leads to