Library · Validation

Data leakage in machine learning

A model leaks when something it was trained with would not be available at prediction time, or was computed with the test data. The score then measures the leak. The most common form hides in a preprocessing step.

By , published

Definition

Kaufman and co-authors define leakage as the introduction of information about the target that should not be legitimately available to learn from. Two families cover most cases. In the first, a feature contains the answer: a field filled in after the event, an identifier assigned by outcome. In the second, a step fitted on all the data, such as scaling, imputation, feature selection or oversampling, lets the test rows influence the training.

Kapoor and Narayanan surveyed published research across 17 fields and listed 294 papers affected by leakage, with conclusions that changed once it was removed.

Worked example

One hundred rows, 5,000 features of pure noise and a random label. In the first run, the 20 features most correlated with the label are selected on all rows, then a classifier is cross-validated. In the second, the selection is inside the pipeline and is redone on each training fold.

import numpy as np
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline

rng = np.random.default_rng(2)
X = rng.normal(size=(100, 5000))
y = rng.integers(0, 2, 100)                      # the label is a coin flip

X_sel = SelectKBest(f_classif, k=20).fit_transform(X, y)          # selection sees every row
leaky = cross_val_score(LogisticRegression(), X_sel, y, cv=5).mean()

pipe = make_pipeline(SelectKBest(f_classif, k=20), LogisticRegression())
clean = cross_val_score(pipe, X, y, cv=5).mean()                  # selection inside each fold

print(f"selection before cross-validation: accuracy = {leaky:.2f}")
print(f"selection inside each fold:        accuracy = {clean:.2f}")

Output, produced by running the code above when this page was built:

selection before cross-validation: accuracy = 0.87
selection inside each fold:        accuracy = 0.59

The label is a coin flip, so the true accuracy of any model is one half. The leaky run reports a score far above it, and the only difference between the two runs is where the selection was fitted. The second run is within the noise that 100 rows allow around one half.

How to find it

  • Put every fitted step inside the pipeline that is cross-validated. If a transformation has parameters, it is a fitted step.
  • Run a placebo: shuffle the target and rerun the whole pipeline. A score above chance on shuffled labels is a leak.
  • Be suspicious of a feature that is too good. Check when its value is written and whether it can change after the event.
  • For time-indexed data, test with walk-forward validation and check the date each value was known, see look-ahead bias.

Leakage is not always visible in the score. A modest leak gives a plausible number, and the model fails only once deployed.

In our work

Every pipeline we deliver is run once on shuffled labels before any result is reported. In our credit model validation, 20 placebo models trained on shuffled labels had a mean AUC of 0.496 on the holdout.

References

  1. Kaufman, S., Rosset, S., Perlich, C., Stitelman, O. (2012). Leakage in data mining: formulation, detection, and avoidance. ACM Transactions on Knowledge Discovery from Data, 6(4), 1-21. doi:10.1145/2382577.2382579
  2. Kapoor, S., Narayanan, A. (2023). Leakage and the reproducibility crisis in machine-learning-based science. Patterns, 4(9), 100804. doi:10.1016/j.patter.2023.100804
  3. Ambroise, C., McLachlan, G. J. (2002). Selection bias in gene extraction on the basis of microarray gene-expression data. Proceedings of the National Academy of Sciences, 99(10), 6562-6566. doi:10.1073/pnas.102102699

Have a question like this one on your own data? Tell us what you need in a few lines.

Send us the brief

Related pages

  • The placebo test: shuffled labelsShuffle the target, rerun everything, and look at the score. With no relation left between inputs and target, an honest pipeline scores at chance level. If it scores better, the pipeline is reading the answer somewhere.
  • Look-ahead bias and point-in-time dataA backtest has look-ahead bias when it uses, at a past date, information that became available later: a revised figure, a late report, a statistic computed on the whole sample. Point-in-time data stores what was known when.
  • Nested cross-validationWhen cross-validation is used for hyperparameter tuning, to pick the best of many settings, the winning score is no longer an honest estimate: it was selected for being high. Nested cross-validation scores the whole procedure, selection included.
All library pages