The two transforms
The first is standardisation: subtract the mean, divide by the standard deviation. The result has mean 0 and standard deviation 1, and is unbounded.
The second is min-max normalisation. The result is squeezed into the interval from 0 to 1, and it is bounded but sensitive to a single outlier.
- : the mean and standard deviation of that feature, over the training set only.
Why it changes the answer at all
Gradient descent takes the same step size in every direction. If one feature ranges over 0 to 1 and another over 0 to 100,000, the loss surface is a long thin valley.
The gradient for each weight is proportional to its feature's magnitude, so the large-scale feature produces enormous updates and the small-scale one produces almost none.
A learning rate small enough to be stable for the first is far too small for the second, and training crawls along the valley floor instead of crossing it.
Scaling makes the valley round, and a round valley can be descended in a few steps.
Which models care and which do not
Distance-based methods care completely: k-nearest neighbours, k-means and SVMs all compute distances, and an unscaled feature dominates every distance.
Regularised models care, because a penalty on the weights is a penalty measured in the units the features happen to use.
Tree-based models do not care at all. A decision tree splits on thresholds within one feature and never compares two features' magnitudes, so any monotonic rescaling gives an identical tree.
The mistake that leaks the test set
- Compute the mean and standard deviation on the training set only.
- Store those two numbers as part of the model.
- Apply them to the validation set.
- Apply the same ones to the test set, and to every request in production.
- Never recompute them on data the model has not been trained on.
Fitting the scaler on the whole dataset before splitting lets information about the test set reach the model, and the result is a score that does not survive deployment.