Sub topic 1
Why feature scale matters
Numeric features often come in very different units. Age is measured in tens, income in tens of thousands, and a pixel value in 0 to 255. Many models treat these numbers as if they were directly comparable, and that assumption causes the problems this chapter describes. Later chapters show how StandardScaler and MinMaxScaler fix them.
Prerequisites
This chapter assumes you have:
- Basic Python and NumPy array operations.
- Familiarity with mean and standard deviation.
- A basic idea of training and test sets in machine learning.
- Beginner familiarity with scikit-learn estimators (objects with
fitandpredict).
The core problem
A feature's scale is the typical size of its values. Two features can carry equally useful information while having scales that differ by a factor of a thousand or more. A model that combines features with sums, products, or distances will be dominated by the large-scale feature unless something compensates for the difference.
Scale differences are not a data-quality problem. Changing income from dollars to thousands of dollars loses no information, but it changes what many models do. A sensible model should not depend on your choice of units. Scaling removes that dependence.
Distance-based models
Euclidean distance adds up the squared difference of each feature. A feature with a large numeric range contributes much more to that sum than one with a small range.
The next example uses three people described by age (years) and income (dollars):
import numpy as np
# columns: age, income
A = np.array([25.0, 50000.0])
B = np.array([60.0, 50500.0])
C = np.array([26.0, 58000.0])
print("raw A-B:", round(float(np.linalg.norm(A - B)), 1))
print("raw A-C:", round(float(np.linalg.norm(A - C)), 1))
# Divide each column by its range over these three people
X = np.array([A, B, C])
ranges = X.max(axis=0) - X.min(axis=0)
a, b, c = (X - X.min(axis=0)) / ranges
print("scaled A-B:", round(float(np.linalg.norm(a - b)), 4))
print("scaled A-C:", round(float(np.linalg.norm(a - c)), 4))On the raw values, A-B is about 501.2 and A-C is 8000.0, so B looks far closer to A. The age gap of 35 years is almost invisible next to the income gap. After rescaling, A-B is about 1.0020 and A-C about 1.0004. Both people now differ from A by roughly one full range in one feature, and the ranking flips. A k-nearest-neighbors classifier or a k-means clustering would give different answers for the two versions of the data.
Gradient-based and regularized models
Distance is not the only place where scale matters.
- Gradient descent. When features have very different scales, the loss surface becomes a long, narrow valley. The learning rate that suits one weight is too large or too small for another, so training zigzags or converges slowly. Scaled features give a rounder surface and faster, more stable convergence.
- Regularization. Ridge, Lasso, and the penalty in logistic regression add a cost on the size of the weights. A feature with a small scale needs a large weight to have the same effect, so the penalty hits it hardest. Which features the model keeps then depends partly on units.
- Interpreting coefficients. When features share a scale, coefficient sizes are roughly comparable. Without that, a coefficient of 0.0001 on income and 3.0 on age says little about relative importance.
flowchart TDThe diagram below summarizes the chain from raw features to a distorted model.
What scaling does and does not change
Scaling applies a column-wise linear transformation: subtract a number, then divide by a number. Three consequences follow.
- The order of values within a column is preserved.
- The shape of the distribution is preserved. A skewed column stays skewed.
- Information is not lost. As long as you keep the two numbers per column, you can undo the transformation exactly.
Scaling does not remove outliers, fix skew, or fill in missing values. Later chapters show how outliers still influence each scaler.
Not every model needs scaling
Tree-based models such as decision trees, random forests, and gradient-boosted trees split on thresholds within one feature at a time. A monotonic rescaling of a column moves the thresholds but leaves the splits unchanged, so scaling usually has no effect on them. The next chapter covers which models are scale-sensitive and which are not.
Key takeaways
- Features with larger numeric ranges dominate distance calculations, slow gradient descent, and receive uneven regularization.
- Changing units should not change a model's behavior, and scaling makes that true for scale-sensitive models.
- Scaling is an invertible linear transformation that keeps ordering and distribution shape.
- Tree-based models are mostly unaffected.
Exercise
A dataset has a column "distance_km" with values from 0 to 20 and a column "salary" with values from 20,000 to 200,000. You fit a k-nearest-neighbors classifier on the raw columns. Which feature will mostly decide who the neighbors are, and why? Would a random forest on the same data have the same problem?
Show answer
Salary will mostly decide the neighbors. Euclidean distance sums squared differences, and salary differences (up to 180,000) are thousands of times larger than distancekm differences (up to 20), so the distancekm term is nearly negligible. A random forest would not have this problem, because each split looks at one feature at a time and is unaffected by a monotonic rescaling of a column.