# 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. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/splits-and-leakage
Last updated: 2026-11-28

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](https://www.internhack.in/roadmaps/tracks/machine-learning/encoding-and-scaling)

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

- [scikit-learn: Common pitfalls and recommended practices](https://scikit-learn.org/stable/common_pitfalls.html) (scikit-learn)
- [Kaggle Learn: Intermediate Machine Learning (data leakage lesson)](https://www.kaggle.com/learn/intermediate-machine-learning) (Kaggle)

Previous: [Encoding Categories and Scaling Numbers](https://www.internhack.in/roadmaps/tracks/machine-learning/encoding-and-scaling) · Next: [Framing ML Problems and the scikit-learn API](https://www.internhack.in/roadmaps/tracks/machine-learning/ml-problem-framing)

## More in From Raw Data to Model-Ready Features

- [Exploratory Data Analysis](https://www.internhack.in/roadmaps/tracks/machine-learning/exploratory-data-analysis)
- [Data Cleaning and Missing Values](https://www.internhack.in/roadmaps/tracks/machine-learning/data-cleaning)
- [Feature Engineering](https://www.internhack.in/roadmaps/tracks/machine-learning/feature-engineering)
- [Encoding Categories and Scaling Numbers](https://www.internhack.in/roadmaps/tracks/machine-learning/encoding-and-scaling)
- [Train/Validation/Test Splits and Data Leakage](https://www.internhack.in/roadmaps/tracks/machine-learning/splits-and-leakage)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
