A store builds a model to predict returns at checkout. Its test score looks excellent. Then someone notices a feature called “refund date” in the training data. The model learned from a field that only appears after the return has already happened.
Data leakage in machine learning happens when training or evaluation uses information that should be unavailable at that stage. It can make a test score look stronger than the model’s real performance. Start by writing down when the model will make a prediction and what it can know at that moment.
Check what was available at prediction time
For the store, the prediction time is checkout. Product category, price, and past orders may all be available. Return authorization and refund dates arrive later. A warehouse scan may also arrive after checkout, even if a data export places it on the same row as the order.
| Feature at checkout | Keep or investigate? | Reason |
|---|---|---|
| Product category | Keep | Recorded by checkout |
| Number of prior returns | Keep with a time cutoff | Count only events known at checkout |
| Refund date on this order | Remove | Recorded after the outcome |
| Whether a return ticket was opened | Remove | Ticket can be created after checkout |

Ask when each source first had the value and when the model could retrieve it. A transaction timestamp and the time a record became available can differ. When reconstructing training rows, use only records available by the prediction cutoff. Google’s guide to checking for label leakage.
Check how the model saw its evaluation data
Preprocessing learned from the full dataset
Split the raw examples before fitting a scaler, imputer, or feature selector. During cross-validation, fit those steps inside each training fold, then apply the fitted transformations to the matching validation fold. A pipeline helps keep this fit-and-transform sequence consistent.
Scikit-learn illustrates the problem with 200 examples, 10,000 random features, and randomly assigned labels. Selecting features on the full dataset gives 76% test accuracy in its example. Splitting first and fitting feature selection only on training rows gives 50%. Feature selection had used test labels to favor chance correlations. Scikit-learn’s documented experiment.

Related records crossed the split
Look for duplicated examples and repeated entities such as multiple rows from the same order, customer, or device. If the model must generalize to new customers, use a customer-grouped split. If the model will score future orders from the same customer base, use a time split and reconstruct each feature at the time an order would have been scored. How to choose a cross-validation split.
Repeated testing shaped model choices
If you pick features, thresholds, or models after reading test results, the test set has influenced development. Do that work on validation data. If the old test set guided several decisions, use a fresh holdout to estimate final performance.
Rebuild the evaluation in this order
- Write the prediction contract. Specify the target, prediction time, forecast window, and the people or records the model will score.
- Trace high-impact features. Check source timestamps, joins, aggregate windows, and fields that are created after the target event.
- Create the split before fitting. Apply the deployment-appropriate grouping or time cutoff, then fit preprocessing and the model within training data.
- Re-run a simple baseline. Compare the old result with the corrected run, then inspect which kinds of errors changed. Record the evaluation split and metric.
A lower score after the repair can be the more reliable estimate. For a rare event such as returns, inspect false alarms and missed returns as well as accuracy. How to read a confusion matrix.
Questions that come up during a leakage audit
Can a feature leak the target even if its name looks harmless?
Yes. A status code, timestamps, or even a missing field can reveal that a later process already happened. Check the underlying system and when it writes the values.
Does a scikit-learn pipeline prevent every kind of leakage?
A pipeline keeps its fitted steps inside each training fold when used correctly. You still have to choose a realistic split and build features from records available at prediction time.
How is leakage related to overfitting?
Both can produce disappointing results after deployment. Overfitting can happen on a clean split when the model learns patterns that fail to generalize. Leakage gives training or evaluation access to information outside its intended boundary. How to recognize overfitting.














