Sub topic 3
Scaling features before clustering
This chapter assumes you know Euclidean distance, basic NumPy, and scikit-learn's fit and fit_transform style API. Hierarchical clustering and DBSCAN both work only from distances between points. If one feature has a much larger numeric range than the others, it controls those distances and the other features barely matter.
Why scale matters
Suppose each customer has an age in years and an income in dollars. A 35-year age gap is a big difference between people. A $1,000 income gap is small. Euclidean distance sees it the other way round, because it adds squared differences in raw units.
import numpy as np
from sklearn.preprocessing import StandardScaler
# columns: age (years), income (dollars)
X = np.array([[25, 50000], [60, 51000], [26, 90000]], dtype=float)
def dist(a, b):
return np.linalg.norm(a - b)
print(dist(X[0], X[1])) # about 1000.6: age differs by 35, income by 1000
print(dist(X[0], X[2])) # about 40000.0: age differs by 1, income by 40000
Xs = StandardScaler().fit_transform(X)
print(dist(Xs[0], Xs[1])) # about 2.15
print(dist(Xs[0], Xs[2])) # about 2.15Before scaling, the first pair looks 40 times closer than the second, and the age gap is almost invisible. After scaling, both pairs are about the same distance apart, and both features contribute.
What scaling does to each method
- Linkage criteria compare distances, so raw units change which clusters merge first and the shape of the dendrogram.
- DBSCAN uses one
epsfor all features. With unscaled data, a single radius cannot suit a feature measured in dollars and another measured in years. - Ward linkage minimizes variance growth, so it is especially sensitive to features with large variance.
Common scalers
| Scaler | What it does | When to prefer it |
|---|---|---|
| `StandardScaler` | Subtracts the mean, divides by the standard deviation | Default choice, roughly bell-shaped features |
| `MinMaxScaler` | Maps each feature to the range 0 to 1 | Bounded features, when you want a fixed range |
| `RobustScaler` | Uses the median and interquartile range | Features with outliers |
Common errors
- Scaling features that are already in the same units, such as pixel intensities. This can exaggerate noise in low-variance features.
- Forgetting that scaling is a modeling choice. If one feature matters more to you, scaling every feature equally treats them all as equally important. You can multiply a scaled column by a weight to adjust this.
- Keeping many redundant features. Several columns measuring the same thing give that quantity extra weight in the distance.
- Using a mean-and-standard-deviation scaler on data with extreme outliers. The outliers inflate the standard deviation and squash everything else. Try
RobustScaler.
Exercise
A dataset has height in centimeters (values near 170) and annual salary in dollars (values near 60000). You run DBSCAN on the raw data with eps=0.5 and nearly every point comes out as noise. Give two reasons scaling might help, and say what you must redo afterwards.
Show answer
Salary dominates the distances, so points differ by thousands of units and almost none fall within 0.5 of each other. Height is effectively ignored. After standardizing, both features have comparable spread, so distances reflect both and a small eps can find neighbors. You must re-choose eps, for example with a k-distance plot on the scaled data, because the old value was tied to the raw units.