Skip to content
Machine Learning
Unsupervised Learning 8 min read

Clustering with K-Means

Learn without labels — step through the K-Means loop, choose k with the elbow and silhouette methods, and know when to reach for DBSCAN instead.

Download notebook Open Google ColabIn Colab: File → Upload notebook → pick the downloaded file.

Everything so far had a supervisor: every row came with the right answer, and the model's job was to match it. Clustering removes the answer key. You hand the algorithm raw points and ask, "which of these belong together?" In this lesson you'll run the K-Means loop by hand, learn how to pick the number of clusters, and see exactly where K-Means breaks — and what to use when it does.

Learning without labels

In supervised learning, "good" means "close to the labels". Without labels, we need a different definition, and clustering uses geometry: a good grouping puts similar points in the same cluster and dissimilar points in different ones. That makes clustering genuinely useful when labels don't exist yet — segmenting customers, grouping documents by theme, compressing colors in an image — but it also means you have to judge whether the clusters mean anything. The algorithm will always give you groups; it can't tell you if they matter.

The K-Means loop

K-Means is the workhorse. You choose k, the number of clusters, and the algorithm alternates two moves until nothing changes:

  1. Assign — give each point to its nearest centroid
  2. Update — move each centroid to the mean of its assigned points

Try it yourself. Step through the iterations below and watch the two moves alternate — and keep an eye on the inertia readout as the centroids settle:

k-means clustering, step by step

Each iteration alternates two moves: assign points to their nearest centroid, then slide each centroid to the mean of its points. Inertia only ever goes down.

00224466881010
iteration0next stepassigninertia
Add points: click the chart

A few things you probably noticed: the assignments and centroids stabilize after just a handful of iterations, the inertia only ever goes down, and if you re-initialize, the final clusters can differ. K-Means is only guaranteed to find a local optimum, which is why scikit-learn's KMeans runs several random restarts (n_init) and keeps the best one by default.

Inertia, and why it always drops with k

Inertia is the quantity K-Means minimizes: the sum of squared distances from each point to its own centroid. Lower inertia means tighter clusters — but be careful using it to choose k. Adding a cluster can only ever reduce inertia (in the extreme, k = n gives inertia 0, with every point as its own "cluster"). So you can't just pick the k with the lowest inertia; you look for the point of diminishing returns.

Choosing k: elbow and silhouette

The elbow method plots inertia against k and looks for the bend — the k after which extra clusters stop paying for themselves. The silhouette score measures, for each point, how much closer it is to its own cluster than to the nearest other cluster (from −1 to +1, higher is better) — and unlike inertia, it peaks at a good k instead of always decreasing:

Python — runs in your browser

The data was generated with 4 blobs, and both diagnostics agree: the elbow bends at 4 and the silhouette peaks there. On real data the signals are rarely this clean — treat them as evidence, not verdicts, and sanity-check the clusters against domain knowledge.

Scale your features first

K-Means is built on Euclidean distance, so a feature measured in thousands (income) will completely drown one measured in single digits (number of purchases). Put a StandardScaler in front of KMeans — in a Pipeline — essentially every time you cluster real data.

Where K-Means fails

The assign-to-nearest-centroid rule carves space into convex regions, so K-Means silently assumes clusters are roughly spherical, similar in size, and separable by straight boundaries. Give it two interleaved crescents and it fails confidently:

Python — runs in your browser

K-Means slices the moons with a straight cut because that's all it can do. Other classic failure modes: clusters with very different densities or sizes (the big cluster "steals" points from the small one), outliers dragging centroids around, and non-numeric data you can't take a mean of (variants like K-Modes and K-Prototypes exist for that).

Beyond K-Means: DBSCAN and agglomerative clustering

Two alternatives cover most of what K-Means can't:

  • DBSCAN grows clusters from dense regions: points with enough neighbors within radius eps seed a cluster, and it expands through connected dense areas. It finds arbitrarily shaped clusters, doesn't need k, and labels sparse points as noise (-1).
  • Agglomerative clustering starts with every point as its own cluster and repeatedly merges the closest pair, building a hierarchy (visualized as a dendrogram). With linkage="single" ("closest points" distance) it chains along curved shapes nicely.
Python — runs in your browser

Both alternatives recover the moons perfectly. So why is K-Means still the default? It's fast, it scales to millions of points, its clusters come with centroids you can interpret and reuse (as you'll see next lesson), and many real datasets — especially after scaling — really are blob-shaped. DBSCAN's weakness is choosing eps and handling clusters of varying density; agglomerative clustering is O(n²) and struggles past tens of thousands of points.

Check your understanding

4 questions · free
  1. Q1.In the K-Means loop, what happens during the 'update' step?

  2. Q2.Why can't you choose k by simply picking the value with the lowest inertia?

  3. Q3.You cluster customers on income (in dollars) and number of visits (0–50) without scaling. What happens?

  4. Q4.Which dataset shape would make DBSCAN a better choice than K-Means?

Exercise: Break K-Means, then fix the diagnosis

Generate blobs with very different spreads: 3 centers with cluster_std=[0.5, 2.5, 0.5] (400 points, random_state=7). Run K-Means with the correct k = 3, then compare its labels to the true ones with a side-by-side scatter plot and compute the silhouette score of both labelings. Which failure mode from the lesson are you seeing?

Next up: putting clusters to work — segmenting customers into named personas and compressing an image down to a handful of colors.