❓ Why Scale Features?

K-Means clustering uses Euclidean distance to measure similarity between customers. If features are on different scales, the algorithm will be biased towards the feature with the largest range.

Example (without scaling):
Annual Income ranges from 15 to 137 (range = 122).
Spending Score ranges from 1 to 99 (range = 98).

A difference of 10 in Income contributes the same distance as a difference of 10 in Spending Score — which may not reflect actual business similarity.
Solution: StandardScaler transforms each feature to mean=0, std=1, making all features contribute equally to distance calculations.
📐 StandardScaler Formula
z = (x − μ) / σ
z = scaled value  ·  x = original value  ·  μ = mean  ·  σ = standard deviation

After scaling: mean becomes 0 and standard deviation becomes 1 for each feature. Values are unitless and directly comparable.

Python 1 from sklearn.preprocessing import StandardScaler
2 X = df[['Annual_Income', 'Spending_Score']].values
3 scaler = StandardScaler()
4 X_scaled = scaler.fit_transform(X)
🎯 Features Selected for Clustering
💰
Annual Income

Annual income in thousands of dollars. Range: 15k–137k. Strong economic indicator.

🛍️
Spending Score

Mall-assigned spending behaviour score (1=low, 100=high). Captures buying habits.

Why only 2 features? Annual Income and Spending Score create clear, interpretable clusters in 2D space. Age adds noise to cluster separation in this dataset, and Gender alone doesn't distinguish economic behaviour.
🔄 Before vs After Scaling — First 8 Rows
Original Values
#Annual IncomeSpending Score
11539
21581
3166
41677
51740
61776
7186
81894
Scaled Values (z-score)
#Annual Income (z)Spending Score (z)
1-1.739-0.4348
2-1.7391.1957
3-1.7008-1.7159
4-1.70081.0404
5-1.6627-0.396
6-1.66271.0016
7-1.6245-1.7159
8-1.62451.7004
Note: Negative z-scores indicate values below the mean. Positive z-scores indicate above mean. K-Means now treats distance equally across both features.