How can data leakage be prevented?
Data leakage can be prevented by designing the machine learning pipeline so that information from the future, the target, or evaluation data cannot influence training.
The main principle is simple: the model should only learn from information that would genuinely be available when a real prediction is made.
Split the data before learning from it
Create the training, validation, and test splits before applying preprocessing steps that learn statistics from the data.
For example, if missing values are filled using a median, calculate that median from the training set only.
Then apply the learned value to the validation and test sets.
Fit preprocessing only on training data
Steps such as these should usually be fitted using training data only:
- scaling
- imputation
- feature selection
- dimensionality reduction
- learned encodings
- aggregate statistics
This prevents validation or test information from leaking into the model-development process.
Respect the prediction-time boundary
For every feature, ask whether it would actually be available when the prediction is made.
Do not use:
- future events
- post-outcome information
- variables created after the target has already occurred
Time-based problems should preserve chronological order.
Keep related records together when necessary
If multiple rows belong to the same customer, patient, device, household, or account, placing related rows in different splits can create leakage.
Use a group-aware split when the real prediction scenario requires those entities to remain separate.
Protect the test set
The test dataset should not be used to:
- choose features
- tune hyperparameters
- select models
- adjust preprocessing
- repeatedly guide development decisions
Use validation data for development decisions and keep the test set for final evaluation.
Use pipelines
Machine learning pipelines can help ensure that preprocessing is fitted inside each training split and then applied correctly to validation or test data.
This is especially important during cross-validation.
Review feature creation carefully
Document how and when each feature is produced.
A feature that looks harmless may still contain future information or a hidden version of the target.
In short, prevent data leakage by isolating evaluation data, fitting learned transformations only on training data, respecting time and group boundaries, and ensuring every feature would truly be available at prediction time.
Learning Sequence
Machine Learning Curriculum
Why is data preprocessing important in machine learning?
Explore Related Questions
Deepen your understanding across related Machine Learning topics.