Fit a scaler using training data only
Return to the home row after each number or separator. · 174 words
A scaler converts numeric features to comparable ranges. One method is the z-score: subtract the training mean and divide by the training standard deviation, a measure of spread. For a bookstore model predicting spending, training age has mean 40 and standard deviation 10, while spending has mean $60 and standard deviation $20. A test customer aged 50 has age z-score (50 - 40) / 10 = 1; $80 spending has z-score (80 - 60) / 20 = 1. The scaler must be fitted on training rows only.
Now predict hotel bookings from distance to the station and nightly price. If training means are 5 km and $120, and standard deviations are 2 km and $30, a validation hotel at 7 km and $150 receives z-scores of 1 for both: (7 - 5) / 2 and (150 - 120) / 30. These same training statistics transform validation and test rows. If test data helped calculate the mean or spread, evaluation information would leak into training and make results look better than they will in production.
Check your recall
If the training age mean is 40 and its standard deviation is 10, what is the z-score for age 50, and which rows determine the mean and standard deviation?
Check your answer
The z-score is (50 - 40) / 10 = 1. The mean and standard deviation are computed from training rows only, then reused unchanged to transform validation and test rows.
Sources and credits
Original KeyTouch lesson. Background reading · Google · Machine Learning Crash Course.