Skip to main content

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.

Mean centering: subtract the fitted mean from every value.
RowOriginalCentered
Training 12.00-2.00
Training 24.000.00
Training 36.002.00
Held-out 114.0010.00
Held-out 218.0014.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.