K-Means Clustering — Assign → Update → Repeat
Use K-Means to partition unlabelled points into exactly clusters by minimising the within-cluster sum of squares (WCSS). It's the default choice for Customer Segmentation and other Unsupervised Learning grouping tasks.
- Initialise — randomly place centroids in feature space.
- Assign — assign each point to the nearest centroid (by Euclidean Distance).
- Update — move each centroid to the mean of its assigned points.
- Repeat steps 2–3 until assignments stop changing (convergence).
Common pitfalls
- You must choose in advance — use the Elbow Method and Silhouette Score to pick it.
- Local optima: K-Means always converges but may land on a local, not global, optimum — run with
n_init=10(the sklearn default) so it tries 10 random starts and keeps the best. - Assumes spherical, equal-size clusters and is sensitive to outliers (means get pulled), so scale features first.
Worked example
From DS Lecture 5 (the StyleNation segmentation, which lands on ):
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=4, n_init=10, random_state=42)
kmeans.fit(X_scaled)
df["cluster"] = kmeans.labels_
print(df["cluster"].value_counts())
See Lecture 5 - Clustering and Dimensionality Reduction for the full walkthrough.