Menu
CoddyTech

Scale Before Clustering

Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 9 of 51.

K-means groups by distance, so the biggest units rule. Shelter cats by weight in grams and age in years: a 500 g gap swamps a 10 year one.

Two charts of 120 shelter cats, age in years across and weight in grams up. Left, clustered raw: the adult cats split by weight, mixing young and old. Right, clustered after scaling: kittens, adults and seniors, split by age

Standardize first, as for k-NN, so each feature counts alike:

X_scaled = StandardScaler().fit_transform(X)
km = KMeans(n_clusters=3, n_init=10, random_state=0)
km.fit(X_scaled)

make_pipeline(StandardScaler(), KMeans(...)) does both in one model. Then read each group in real units: average its original rows, X[km.labels_ == c].mean(axis=0).

challenge icon

Challenge

Easy

A flat-hunting app groups 100 listings by rent and distance to the station. The starter clusters the raw columns, and its groups all sit about 3 km out: dollars drown out kilometers.

  1. Scale the two columns with a StandardScaler.
  2. Cluster the scaled rows instead. Keep the report: it reads each group in real units.

For another city:

38 flats: $1820 a month, 0.9 km out
33 flats: $1130 a month, 4.2 km out
29 flats: $2760 a month, 1.5 km out

Try it yourself

import pandas as pd
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler

# One row per flat: monthly rent in dollars, distance to the station in km
flats = pd.read_csv("flats.csv")
X = flats[["rent", "station_km"]]

# This clusters the raw columns: fix it
km = KMeans(n_clusters=3, n_init=10, random_state=0)
km.fit(X)

# Report each group in real units, biggest first
sizes = pd.Series(km.labels_).value_counts()
for c in sizes.index:
    group = flats[km.labels_ == c]
    print(f"{sizes[c]} flats: ${round(group['rent'].mean())} a month, {group['station_km'].mean():.1f} km out")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unsupervised Learning

Practice on your own: Online Python compiler