Stop 11 · Unsupervised LearningTopics · 2/10

Choosing K (Elbow & Silhouette)

Part of the Machine Learning Roadmap

In short

K-means will happily split your customers into 3 groups, or 30. It never tells you which number is right. The elbow method and silhouette score give you evidence, and business sense makes the final call.

K-means needs you to choose the number of clusters. The elbow method looks for where adding clusters stops reducing within-cluster distance much, and the silhouette score measures how well each point fits its own cluster compared with the nearest other one.

The jaadu places small toy buses on a big floor map of a city, grouping clusters of tiny houses around each bus.

Think of it like this

How many school bus routes?

A school in Jaipur plans bus routes. One bus means children travel for hours. Fifty buses means every child gets a private ride, which is absurd. Adding buses cuts travel time a lot at first, then barely at all. The point where extra buses stop helping much is the elbow. The silhouette asks: is each child clearly closer to their own bus's stops than to the next route's?

Watch it happen

Inertia and silhouette for k = 2 to 6

k=2
0
k=3
1
k=4
2
k=5
3
k=6
4

1/5We run k-means five times and record two numbers for each k.

The big ideas

What you need to know

Elbow uses inertia

Inertia always falls as k grows. Look for where the drop slows sharply. Sometimes there is no clear elbow.

Silhouette per point

s = (b - a) / max(a, b), where a is the average distance to its own cluster and b to the nearest other cluster. It ranges from -1 to 1.

Higher silhouette is better

Near 1 means well separated, near 0 means on a border, negative means probably in the wrong cluster.

Business sense decides

A marketing team can act on 4 segments, not 17. Use the metrics to shortlist, and usefulness to choose.

See it in code

Walk through the code

python
1import matplotlib.pyplot as plt2from sklearn.cluster import KMeans3from sklearn.datasets import make_blobs4from sklearn.metrics import silhouette_score5from sklearn.preprocessing import StandardScaler6 7X, _ = make_blobs(n_samples=600, centers=4, cluster_std=1.2, random_state=42)8X = StandardScaler().fit_transform(X)9 10ks = range(2, 9)11inertias, sils = [], []12for k in ks:13    km = KMeans(n_clusters=k, n_init=10, random_state=42).fit(X)14    inertias.append(km.inertia_)15    sils.append(silhouette_score(X, km.labels_))16 17fig, ax = plt.subplots(1, 2, figsize=(10, 4))18ax[0].plot(ks, inertias, marker="o")19ax[0].set(xlabel="k", ylabel="Inertia", title="Elbow")20ax[1].plot(ks, sils, marker="o")21ax[1].set(xlabel="k", ylabel="Silhouette", title="Silhouette")22plt.show()

1/5Synthetic data with 4 blobs, scaled so no axis dominates the distances.

Data built with 4 true groups. Both plots should point to k = 4.

Save yourself the pain

Common mistakes

Oops

Picking the k with the lowest inertia.

Do this instead

Inertia keeps falling until every point is its own cluster. Look for the elbow, not the minimum.

Oops

Running k-means on unscaled features like income and age.

Do this instead

Scale first. Otherwise the column with the biggest numbers decides the clusters alone.

Your mission

Find k on real data

~25 min

Quick check

A point has a silhouette score of -0.3. What does that suggest?

Go deeper

  1. 1Plot inertia for k from 2 to 10 and look for the elbow.
  2. 2Compute silhouette_score for the same range and compare the best k.
  3. 3Check whether the chosen clusters make business sense, not just a good score.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge