Module 8: Finding patterns without labels · Lesson 8.2 · about 20 min
Choosing k: elbow and silhouette
Inertia always falls as k grows, so it cannot pick k alone. Look for the elbow, or for the peak of the silhouette score, which compares each point's own cluster with the next nearest. Then ask whether the clusters are useful.
Module 8 · about 20 minutes · read first: 8.1. You’ll be able to read an elbow plot, compute a silhouette score by hand, and choose k with both the numbers and the use in mind.
1. Why this matters
k-means needs before it starts. But the “right” number of customer segments, store types or document topics is exactly what you don’t know. Choose badly and you get segments that are either too vague to act on or too many to manage. The two plots in this lesson appear in nearly every clustering report.
2. The elbow
Run k-means for and plot the inertia (Lesson 8.1). It always falls. But if the data has real groups, it falls steeply up to , because each new centre takes over a whole group. After that it falls slowly, because new centres just split groups that were fine. The bend is the elbow.
Think of hiring cashiers for a shop. The first few cut the queue a lot. After a point, each extra cashier saves only seconds. The elbow is where hiring stops paying.
The elbow is easy to explain, but it is often soft: on real data the curve may bend gently, and two people will point at different .
3. The silhouette
The silhouette asks each point: are you closer to your own group than to the next-nearest group? For each point:
- = mean distance to the other points in its own cluster,
- = mean distance to the points of the nearest other cluster,
- .
is near 1 if the point sits snugly in its cluster, near 0 if it is on a border, and negative if it is probably in the wrong cluster. Average over all points, and choose the with the highest average.
Watch it happen
One shopper, three segments.
Own group. Her distances to the other members of her own segment are 1, 2 and 3. So a = (1 + 2 + 3) ÷ 3 = 2.
Other groups. To segment 2: 4, 5, 6 (average 5). To segment 3: 7, 8, 9 (average 8).
Nearest neighbour group. b is the smaller average: 5. Segment 2 is where she would go if her own segment vanished.
Her silhouette. s = (5 − 2) ÷ max(2, 5) = 3 ÷ 5 = 0.6. Comfortably inside her own segment.
Score the whole k. Do this for every shopper and average. Repeat for k = 2, 3, 4, ... and pick the k with the highest average.
Rough guide for the average silhouette: above 0.7 strong structure, 0.5 to 0.7 reasonable, 0.25 to 0.5 weak, below that little real structure.
- a(i)
- how far point i is, on average, from its own group-mates: small is good
- b(i)
- how far it is, on average, from the nearest other group: big is good
- s(i)
- the gap between the two, scaled to lie between −1 and 1
- Average s
- one score for the whole clustering, for comparing different k
- Physical meaning
- Seating guests at a wedding. A good seating plan has everyone closer to their own table-mates than to the next table. A guest with s near 0 could sit at either table; a guest with negative s is at the wrong one.
4. The question numbers can’t answer
Clusters are useful if they help a decision. A marketing team that can run four campaigns may want even if the silhouette prefers 3. Look at each cluster’s averages, give it a name (“young commuters”, “weekend families”), and check that the groups are stable: rerun with another seed or on a sample of the data and see if they come back.
“There is one correct k, and the elbow tells you what it is.” Most real data, such as customers, has no sharp borders: shoppers shade from one type into another. Then the elbow is soft, the silhouette is low for every k, and several choices are equally defensible. The numbers narrow the range; usefulness and stability make the final choice.
Maths and principle
Principle: good clusters are tight inside and far apart.
Silhouette of point in cluster , with the distance:
Why inertia always falls: with one more centre, you could put it on any point and leave the rest as they were; that alone lowers the total. The best solution is at least that good.
Analogy: seating guests at tables. A good seating has everyone closer to their own table-mates than to the next table.
Try it: a point’s mean distance to its own cluster is 2, and to the nearest other cluster 5. Silhouette? (.)
5. Worked example
Made-up numbers. Clustering 2,000 shoppers by visits, spend and basket size (scaled):
| k | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|
| inertia | 3,900 | 2,600 | 1,700 | 1,520 | 1,390 |
| silhouette | 0.41 | 0.47 | 0.53 | 0.46 | 0.42 |
Inertia drops by 1,300, then 900, then only 180: the elbow is at 4. The silhouette also peaks at 4 (0.53, reasonable structure). Four segments it is, provided the team can describe each one and the same four come back with another seed.
6. Practice
- In the simulation, set 3 real groups with small spread. Where are the elbow and the silhouette peak?
- Widen the groups to 0.6. What happens to both signals?
- Can the silhouette help choose ?
Answers
- Both at k = 3.
- The elbow softens and the silhouette falls and flattens: the groups overlap, and there is no clear answer.
- No: with one cluster there is no “other cluster”, so is undefined. Use it to compare k = 2 and up.
Try it on your own data: the small model trainer draws an elbow plot of inertia for k-means on your CSV.
New words: elbow method, silhouette score, cluster stability, segment.
- Inertia always falls as k grows, so it can’t choose k on its own.
- The elbow is the k after which extra clusters stop cutting inertia much; it is often soft.
- The silhouette compares each point’s distance to its own cluster (a) with the nearest other cluster (b): s = (b − a)/max(a, b). Pick the k with the highest average.
- The final k should also give clusters you can name, use and reproduce with another seed.
Check yourself
Sources
- Rousseeuw (1987), Silhouettes: a graphical aid to the interpretation and validation of cluster analysis, Journal of Computational and Applied Mathematics 20, 53-65 https://doi.org/10.1016/0377-0427(87)90125-7
- James, Witten, Hastie, Tibshirani and Taylor (2023), An Introduction to Statistical Learning with Applications in Python, Springer, Section 12.4 (clustering methods) https://www.statlearning.com/
- scikit-learn user guide, Clustering performance evaluation: silhouette coefficient https://scikit-learn.org/stable/modules/clustering.html#silhouette-coefficient
Your ticks stay in this browser. Save them as a file on the course home.
Updated 2026-11-22. Numbers in the examples are made up to show the idea unless a source is given. Simulations run in your browser; nothing you type is sent anywhere.