What is data leakage in machine learning?
Data leakage in machine learning is when a model accesses, during training, information that will not be available at prediction time in real use, such as test data or a feature derived from the target, so it looks far better in evaluation than it really is.
Unlike data contamination, where evaluation examples leak into large language model training data, data leakage is a broader error arising in any model at any step from feature selection to preprocessing and splitting.
- Topic
- Machine learning, models and training
- Also known as
- Data leakage, target leakage
How data leakage in machine learning works
- Target leakage: using a field created after the outcome, such as cancellation date when predicting cancellation.
- Preprocessing on all data: normalisation or feature selection including test data passes test information into training.
- Faulty splits: records of the same customer, or future records in a time series, appear in both training and test.
- Prevention: learn preprocessing on training data only, split by time and entity, investigate suspiciously high results.
In more detail
Isolating preprocessing in a pipeline
Preventing preprocessing leakage in production code requires encapsulating scalers, encoders, imputers and feature selectors inside an isolated pipeline (such as scikit-learn's `Pipeline`) so that `.fit()` runs strictly on each training fold and only `.transform()` applies to validation or test splits. Running scaling or target encoding on the full dataset before splitting is one of the most common engineering bugs.
Point-in-time joins
In enterprise relational databases, historical columns are frequently overwritten in place; querying current tables for past training dates leaks future state. Point-in-time (`as-of`) joins and immutable event logs or feature stores ensure that rolling aggregates (e.g., 30-day arrears or average balance) use only events recorded strictly before the prediction timestamp.
Duplicates and leakage from repeated tuning
Leakage also arises from duplicate or near-duplicate samples (for example, augmented images or repeated support tickets) appearing in both training and test splits and from repeated iterative tuning on the same validation set ("adaptive overfitting"), which gradually leaks test-set feedback into architectural choices.
Commonly confused terms
| Term | How it differs |
|---|---|
| Overfitting | Overfitting occurs when a model memorises noise within legitimate training features and therefore performs poorly on test data. With data leakage, the model scores artificially high on the test split too, failing only when deployed to live production where the leaked signal vanishes. |
| Training-serving skew | Training-serving skew refers to bugs or subtle differences between offline training feature pipelines and live serving code. Data leakage is a specific training and evaluation validity flaw where future or out-of-scope information inflates offline validation metrics. |
Why data leakage in machine learning matters for on-premises AI
Undetected leakage makes a model perform far worse in production than expected. A study of machine learning-based science found leakage errors in 17 fields affecting 294 papers and classified leakage into 8 types from textbook errors to open research problems. In enterprise projects, ask business experts whether each feature exists at prediction time.
When its loan default model scores unexpectedly high on test data, a bank reviews features; it removes a field containing collection information created after application and re-splits data chronologically by application date.
Common questions
How is data leakage detected?
Signs are unexpectedly high test results, one feature dominating importance and a marked drop in production performance.
Are data leakage and data contamination the same?
No. Contamination is evaluation examples entering training data. Leakage is any information unavailable at prediction time entering training.
Related terms
Sources
- Kaufman, Rosset and Perlich, Leakage in data mining: Formulation, detection and avoidance, ACM TKDD, 2012
- Kapoor and Narayanan, Leakage and the Reproducibility Crisis in ML-based Science, 2022
In the glossary: Glossary, D to L