Evaluating clusters
Silhouette, elbow and domain knowledge: how to judge clusters when there is no right answer to compare against.
Lesson 49 of 6610 minIntermediate
You will learn to
- Read silhouette scores and plots
- Use the elbow method with care
- Validate clusters with domain knowledge
Before this lesson
Intuition
In supervised learning you compare predictions with the truth. In clustering there is no truth to compare with. Two analysts can cluster the same customers into three groups or into five, and both can be reasonable. So "evaluating" clusters means asking three questions:
- Are the groups tight and separate? Rows in a cluster should be close to each other and far from other clusters. This is what the silhouette and the elbow measure.
- Are they stable? If you re-run with another seed or a slightly different sample, do similar groups appear?
- Are they useful? Can someone describe each group and act differently on it? This is the most important question and no formula answers it.
Picture it
When the dashed lines are much longer than the solid ones, the row clearly belongs where it is. If they are about the same length, the row is on a boundary.
The math
For each row , let be the mean distance to the other rows of its own cluster and the smallest mean distance to the rows of any other cluster. The silhouette of the row is:
Near 1: well inside its cluster. Near 0: on a boundary. Negative: probably closer to another cluster. The score for a clustering is the mean of over all rows.
The elbow method plots the within-cluster sum of squares against the number of clusters . It always decreases as grows (at it is zero). You look for a bend after which extra clusters buy little.
Two other scores appear on the leaderboard: Calinski-Harabasz (ratio of between-cluster to within-cluster spread, higher is better) and Davies-Bouldin (average similarity of each cluster to its closest one, lower is better).
Worked example
Use the six values from the clustering lesson: 1, 2, 4, 9, 10, 12.
Elbow. The within-cluster sum of squares for each (computed by hand, best grouping each time):
| k | Within-cluster sum of squares |
|---|---|
| 1 | 105.33 |
| 2 | 9.33 |
| 3 | 5.17 |
Going from 1 to 2 clusters removes 96 of the 105; going from 2 to 3 removes only 4. The bend is at . On real data the bend is rarely this clear.
Silhouette for k = 2, with clusters 4 and 12:
| Value | a | b | Silhouette |
|---|---|---|---|
| 1 | 2.00 | 9.33 | 0.786 |
| 2 | 1.50 | 8.33 | 0.820 |
| 4 | 2.50 | 6.33 | 0.605 |
| 9 | 2.00 | 6.67 | 0.700 |
| 10 | 1.50 | 7.67 | 0.804 |
| 12 | 2.50 | 9.67 | 0.741 |
The mean silhouette is 0.743. The lowest value, 0.605, belongs to 4, the point nearest the other cluster. That is what you expect: it is the boundary point.
How mlflow does it
- Clustering runs report Silhouette, Calinski-Harabasz and Davies-Bouldin (lower is better for the last one). Homogeneity, Rand index and Completeness are also in the metric catalog; they compare against known labels, so they only make sense if you have labels for checking.
- Plots on a model candidate: Elbow curve, Silhouette score (the score across different numbers of clusters), Silhouette per cluster, Cluster sizes and a 2-D embedding coloured by cluster.
- Rows are never scored against a target in clustering, so there is no cross-validation setting for it.
See the Clustering task page and the metrics reference.
Common mistakes
- Choosing the number of clusters from one number. Combine the elbow, the silhouette and a sanity check by someone who knows the data.
- Comparing silhouette across different scalings. The score depends on the distance, so compare only runs prepared the same way.
- Trusting the 2-D picture. A projection can make clusters look separate or merged when they are not. See Dimensionality reduction.
- Ignoring tiny clusters. A cluster of three rows may be outliers, not a segment. Check the cluster sizes.
- Never testing stability. Re-run with a different seed. If the groups change a lot, the structure is weak.
- Stopping at the metrics. A model with a lower silhouette that the business can act on beats a higher score nobody can use.
Try it in mlflow
Open the silhouette plot
- Open Runs and open a finished clustering run, for example from the Customer segmentation use case.
- Open a model candidate and go to Plots.
- Open Elbow curve, then Silhouette score, then Silhouette per cluster.
- Check Cluster sizes: very small clusters deserve a closer look.