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.
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
EasyA 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.
- Scale the two columns with a
StandardScaler. - 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 outTry 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")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unsupervised Learning
1Finding Groups
Learning Without LabelsMeasuring SimilarityK-Means by HandK-Means in scikit-learnRecap: Gym Members7PCA in Practice
Seeing 64 DimensionsComponents as PatternsCompress and RebuildPCA Before a ModelRecap: Face Scanner10Project: Digit Explorer
Look at the DigitsShrink with PCACluster the DigitsName the ClustersFind the Odd OnesPractice on your own: Online Python compiler