What is feature scaling in machine learning?
Feature scaling transforms numeric input features so they share a similar range. The two most common methods are min-max normalization, which maps each feature to [0, 1], and standardization, which shifts each feature to mean 0 and standard deviation 1. Scaling changes the units of the features but keeps the order of the values within each one.
Scaling matters for any model that compares features by distance or learns them with gradient descent. Without it, a feature such as income in dollars swamps a feature such as age in years. k-nearest neighbors, k-means, support vector machines, PCA, and neural networks all depend on it. Tree-based models such as random forests and gradient boosting do not.
Key ideas
- Feature scaling puts numeric features on comparable ranges.
- Min-max normalization maps a feature to [0, 1] using its minimum and maximum.
- Standardization gives a feature mean 0 and standard deviation 1.
- Unscaled features stretch the loss surface and slow gradient descent.
- Distance-based and gradient-based models need scaling; tree-based models do not.
- Scaling statistics must be computed on the training set only.
Common questions
Which is better, normalization or standardization?
Standardization is the usual default, especially for linear models, SVMs, and neural networks, and it copes better with outliers. Min-max normalization suits features with known fixed bounds, such as pixel intensities, or models that expect inputs in [0, 1]. Trying both and comparing validation scores settles the question for a given dataset.
Should scaling be done before or after splitting the data?
After. Fit the scaler on the training set only, then apply the same transformation to the validation and test sets. Computing the mean, standard deviation, minimum, or maximum on the full dataset leaks information about the test data into training and inflates test scores.
Does the target variable need to be scaled?
For classification, no. For regression with a neural network, standardizing or log-transforming a target with large values often makes training more stable. Predictions then have to be converted back to the original units with the inverse transformation.
Does batch normalization replace input scaling?
Not entirely. Batch normalization rescales the activations inside a network during training, which reduces sensitivity to scale in later layers. Scaling the raw inputs is still standard practice, because it is cheap and keeps the first layer well conditioned.
Related explainers
- Data Preprocessing — Cleaning, scaling, and encoding raw data
- Gradient Descent — Optimizing by following the slope downhill
- K-Nearest Neighbors — Classification by proximity and distance metrics
- Batch Normalization — Why normalizing activations speeds up deep network training
- Norms & Distance Metrics — L1, L2, cosine similarity — measuring closeness in vector spaces
- Dimensionality Reduction — PCA and t-SNE — seeing high-dimensional data
References
- LeCun, Bottou, Orr & Müller (1998), "Efficient BackProp"
- Goodfellow, Bengio & Courville (2016), "Deep Learning", chapter 8: Optimization for Training Deep Models
- Stanford CS231n, "Neural Networks Part 2: Setting up the Data and the Loss"
- scikit-learn User Guide, "Preprocessing data"
Last updated