Designing Experiments › Module 3: Fair comparisons in ML

Data leakage

Lesson 3 of 3 · 2 min

Previous

Data leakage happens when information from the test data sneaks into training. The model then looks better than it really is, because in a sense it has seen the answers.

How common is it?

In 2023, Sayash Kapoor and Arvind Narayanan reviewed ML-based research across many fields. They found leakage problems affecting 294 papers in 17 fields, from medicine to political science. In several cases, fixing the leak made the claimed advantage of complex models disappear.

Common kinds

  • Duplicates: the same or near-identical items in train and test (the same patient's scans, the same photo resized).
  • Preprocessing on all data: normalising, selecting features or balancing classes before splitting, so test statistics shape training.
  • Time travel: using future information to predict the past, like a feature recorded after the outcome.
  • Tuning on the test set: choosing models or settings by test score.

How to prevent it

  1. Split first, then do everything else using training data only.
  2. Split by the right unit: by patient, by user, by time, not just by row.
  3. Check for duplicates across splits.
  4. Touch the test set once, at the end.
  5. Be suspicious of results that are too good.

RememberSplit first, split by the right unit, and keep the test set locked until the very end.