Computing Library › Machine Learning
Machine Learning

Feature Scaling

Feature scaling puts numeric features on a comparable range so distance- and gradient-based models are not skewed by units.

Why scale at all

Many algorithms treat features by their magnitude. If one feature ranges 0 to 1 and another 0 to 100000, the large one dominates distances and gradients, drowning out the small one regardless of importance. Feature scaling rewrites features onto a comparable range so each contributes fairly.

Common methods

Kronos motion — confinement scaling
python
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit(X_train)   # fit on train only
X_train = scaler.transform(X_train)
X_test  = scaler.transform(X_test)

Which models need it

Fit on training only

The scaler's parameters (mean, standard deviation, min, max) must be computed from the training data and then applied to validation and test data. Computing them on the full dataset leaks information from the test set into training, an instance of data leakage. Wrapping scaling in a pipeline ensures it is refit correctly inside each cross-validation fold.