Why must features be scaled before running k-means or DBSCAN?
Last updated
Both algorithms rely on raw distances. A feature with a large numeric range dominates the distance and effectively decides the clusters on its own, regardless of how informative it actually is.
Example: Take income (0–200,000) and age (0–100). A 1-unit income gap is trivial, yet the income axis is 2,000× wider, so clusters are split almost purely by income.
k-means: It minimizes squared Euclidean distance, so large-variance features dominate the objective quadratically.
DBSCAN: A single
epsradius applies to all dimensions. Unscaled, that radius is huge on some axes and tiny on others, so no singleepsmakes sense.Common choices:
StandardScaler: z-scores, the usual default.MinMaxScaler: bounded range, but sensitive to outliers.RobustScaler: median/IQR, better with outliers.
Caveat: scaling is itself a weighting decision. Equalizing variance assumes all features matter equally, so domain knowledge may justify deliberate reweighting.
[lang:python]
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
model = make_pipeline(StandardScaler(), KMeans(n_clusters=4, n_init=10))
labels = model.fit_predict(X)