Data leakage
Leakage uses information that should be unavailable to fitting, selection or the prediction.
On this page 8 sections
01Understand the idea#
Data leakage occurs when information unavailable at genuine prediction time influences model fitting or selection. Evaluation then answers an easier problem than the one the model will face. Leakage can enter through preprocessing, duplicated records, future data, or repeated decisions based on a test set.
Data leakage lets information shape a prediction or its evaluation when that information should be unavailable. Preprocessing is part of the fitted procedure: even a mean learned without labels can use evaluation information.
Training values 2 and 6 give mean 4. Center a held-out value of 10 by subtracting that fixed mean: . Including 10 while fitting the mean would leak held-out information.
Suppose you standardize features using means computed from all rows, then split into training and test sets. Test observations already influenced the means. Instead fit the means on training rows, store them, and apply those same means to the held-out rows without refitting.
Center by the training mean. Training: 0, 4. Held-out value: 8. Fitted mean / centered value?
Show answer and explanation
Mean 2; 8−2=6.
02A feature must also exist when the prediction is made#
A feature must also exist when the prediction is made. At time 4, a reading available at time 3 is usable; one first available at time 7 is not. Availability exactly at the cutoff is allowed here.
Predict at time 6. Feature availability: a at 2, b at 9, c at 5. Usable IDs?
Show answer and explanation
a,c available by 6.
03For each cross-validation fit, learn preprocessing on that fold’s training blocks#
For each cross-validation fit, learn preprocessing on that fold’s training blocks. Apply those learned settings unchanged to its validation block. Fitting a separate scaler on validation data still uses information outside the training fit.
Suppose test block J chooses the best setting. J has now shaped the model. If independent block K is still unused, freeze the procedure and evaluate on K. Renaming J cannot make it untouched.
Test D chose settings. Independent E is unused. “D is untouched.” Repair final evaluation.
Show answer and explanation
D shaped selection.
04For time-ordered predictions, splitting randomly can expose future information#
For time-ordered predictions, splitting randomly can expose future information. For multiple rows from the same person or source, splitting related rows across sets can also inflate performance. Choose a split that reflects what will truly be new when the model is used.
Trace when each value becomes available and which data shape every fitted or selected step.
- Fit preprocessing within each training fold.
Sources & further reading
- [1]Scikit-learn User Guide, Common Pitfalls: Data Leakage ↗Scikit-learn developers · Article
- [2]Scikit-learn User Guide, Cross-validation: Evaluating Estimator Performance ↗Scikit-learn developers · Article