Data Leakage

Fit the scaler before the split and the accuracy goes up. Nothing got better; the number just stopped meaning anything.

Data Leakage

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

Leakage is any path by which information about the evaluation data reaches the model before it is evaluated.
It never raises an error. The only symptom is a score that is better than it should be, which nobody investigates.
The commonest form is fitting a preprocessing step on the whole dataset. The mean and standard deviation are learned parameters.
The second commonest is a feature that could not exist at prediction time — a field filled in after the outcome was known.

Overview

Data leakage happens when model fitting or a development decision uses information that would not be available at prediction time. It can make validation results look better than the model’s real performance, even when no usable signal exists.

In this guide
  1. Watch feature-selection leakage
  2. Keep learned transformations inside the split
  3. Leakage can come from the data definition
  4. A practical check
  5. Sources and next steps

Watch feature-selection leakage

The visualizer generates random labels and noise features. Choose Once, dataset to select features using every row before cross-validation. Because selection has seen labels from later validation folds, some noise features may look predictive by chance and raise the reported score above the chance baseline.

Now choose Inside each fold. Feature selection is repeated using only that fold’s training rows, then evaluated on its held-out rows. This is one random simulation, not a fixed expected score; with random data, results vary around chance.

This shows how leakage can enter through a model-selection decision, not only through a column that directly reveals the answer.

Keep learned transformations inside the split

Suppose a scaler computes a mean and standard deviation using the full dataset, or an imputer estimates a median from all rows. Validation and test examples have influenced the transformation before evaluation. Fit preprocessing on the training fold only, then apply the fitted transform to held-out data. A cross-validation Pipeline makes that order explicit.

Feature selection, target encoding, dimensionality reduction, and resampling also learn from data and must be fitted inside each training fold. A fixed transformation such as converting Celsius to Fahrenheit has no learned parameters, so it does not create this form of leakage.

Leakage can come from the data definition

  • Future information: a customer-status field updated after the event being predicted may reveal the outcome.
  • Related records across splits: repeated scans from one patient or duplicate documents can make validation examples nearly identical to training examples. Split by patient or document group when deployment requires generalizing to unseen groups.
  • Time order: random splitting can train on future periods and evaluate on earlier ones. Use a forward time split when predicting the future.
  • Target construction: a feature derived from the label, directly or indirectly, can encode the answer.

These problems cannot be solved by a pipeline alone. Define what information exists at the moment of prediction and ensure the split reproduces that setting.

A practical check

For every feature, ask when it is recorded and whether it could change after the prediction point. For every preprocessing step, ask which rows were used to fit it. For every split, ask whether a related entity or a later date appears on both sides. Document the answers before trusting a score.

Sources and next steps

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why does fitting a StandardScaler before the train/test split count as leakage?

  2. What is the right question to ask about a suspicious feature?

  3. Why does a random split leak on time-series data?

Cheat sheet

Data Leakage

Spot data leakage in a pipeline before it inflates your metrics. See train-test contamination, target leakage, and preprocessing leaks with examples.

MACHINE LEARNING · vizlearn.in/machine_learning/data_leakage.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.