Train/Validation/Test Splits and Data Leakage

Designing splits that mimic real deployment and spotting the leakage that makes offline results look far better than reality.

Intermediate · about 5 hours · difficulty 3/5

Learn first: Encoding Categories and Scaling Numbers

Your evaluation is only as honest as your split. The test set simulates the future; if information from it (or from the future) sneaks into training, your metrics lie.

Split strategies

  • Train / validation / test: train fits parameters, validation guides choices, test is touched once at the end.
  • Stratified splits keep class proportions stable for classification.
  • Group splits: all rows from one patient, user or device stay in the same split (GroupKFold).
  • Time-based splits: train on the past, validate on the future. Random splits on time series are almost always wrong.

Kinds of leakage

  • Target leakage: a feature that is only known after the outcome (for example refund_issued when predicting fraud).
  • Train-test contamination: fitting scalers, imputers or feature selection on all data before splitting.
  • Duplicate or near-duplicate rows across splits (common with images and user records).
  • Temporal leakage: aggregates or normalizations computed with future data.
  • Test set overuse: tuning repeatedly against the test set turns it into a validation set.

Good enough to move on when, for any new problem, you can state what one prediction looks like in production, what data is available at that moment, and design a split that mirrors it.

Key concepts

  • Holdout splits
  • Stratified splitting
  • Group-aware splits
  • Time-based splits
  • Target leakage
  • Preprocessing leakage
  • Adversarial validation: Detects train/test distribution shift

Mini project

Deliberately build a leaky pipeline on a public dataset (scale and select features before splitting, random split on time-ordered data), measure the inflated score, then fix each leak and report the honest score.

Check yourself

You have multiple X-ray images per patient. Why does a random image-level split overestimate performance?

Free resources