Lesson 1 of 3 · About 5 minutes · Data leakage
Your preprocessing learns from data, too
You split your rows correctly, but computed the mean before splitting. Held-out information has already reached the training pipeline.
Try it
Choose which rows supply the mean. Then change a held-out value. Can that change the centered training values? Try both fitting choices.
Which rows set the mean?
Predict whether changing a held-out value will change the centered training values. Move the slider, then compare the two fitting rules.
Training values stay fixed at 2, 4, and 6. Use the arrow keys to adjust.
Fitted mean: 4.00 · 3 training rows
Held-out rows are excluded from fitting. Changing either one leaves the mean and the centered training values unchanged.
| Row | Original | Centered |
|---|---|---|
| Training 1 | 2.00 | -2.00 |
| Training 2 | 4.00 | 0.00 |
| Training 3 | 6.00 | 2.00 |
| Held-out 1 | 14.00 | 10.00 |
| Held-out 2 | 18.00 | 14.00 |
Transforming held-out rows with a training-fitted mean is expected. This example only centers values; it does not scale their spread or measure prediction accuracy.
Mean-centering learns a mean, just as a model learns coefficients. Fit it on training rows, then subtract that same mean from both training and held-out rows. Full standardization also learns a scale; imputation and feature selection can learn other quantities. Keep each learned step inside the training boundary. Leakage can bias an evaluation, but does not guarantee a better reported score for every dataset or model.