Sub topic 1
Why Feature Scale Matters
Prerequisites
Assume you know basic Python, NumPy arrays, mean, standard deviation, and the purpose of training and test sets.
Why feature scale matters
A feature is one measurable input column, such as age or income. Features often use different units. For example, age might range from 18 to 80, while income might range from 20,000 to 200,000.
Large numeric values can dominate calculations even when they are not more important. A distance-based model comparing age and income may treat income as the main signal because its numbers are larger. Gradient-based models can also take inefficient steps when features have very different scales. Regularized models may penalize coefficients unevenly for the same reason.
Scaling changes the numeric representation without changing the ordering of values or the underlying observations.
Which models need scaling and which do not
Scaling usually matters for models that use distances, gradients, or coefficient penalties.
| Scaling often matters | Scaling often does not matter |
|---|---|
| k-nearest neighbors, support vector machines, neural networks, linear and logistic regression with regularization | Decision trees, random forests, and other tree-based models |
Always check the model and preprocessing requirements. Scaling is usually harmless for tree models, but it adds an unnecessary transformation.
Standardization: centering and scaling to unit variance
Standardization converts each value to a z-score:
z = (x - mean) / standard deviation
The transformed column has mean near 0 and standard deviation near 1. A z-score describes how far a value is from its column mean in standard-deviation units.
import numpy as np
X = np.array([[1.0, 200.0],
[2.0, 400.0],
[3.0, 600.0]])
z = (X - X.mean(axis=0)) / X.std(axis=0)
print(z)Standardization is a common default for distance-based, gradient-based, and regularized models. It does not force every value into a fixed minimum and maximum.
Min-max scaling to a fixed range
Min-max scaling maps each column to a chosen interval, commonly 0 to 1:
x_scaled = (x - minimum) / (maximum - minimum)
The smallest training value becomes 0, and the largest becomes 1. This fixed range can help models or inputs that expect bounded values. Values outside the training range can become less than 0 or greater than 1 unless clipping is enabled.
Both methods must learn statistics from training data only. Applying the same learned statistics to later data prevents information leakage.
Exercise
Which scaler maps a column's observed minimum to 0 and maximum to 1?
Show answer
Min-max scaling, when its feature range is (0, 1).