Clusters have no labels to check against, so how do we know whether a clustering is any good? Validation measures answer this and are divided into internal and external kinds.
Internal Measures
Internal measures use only the data and the clusters. Inertia (SSE) measures compactness but always falls as k grows, so it is used with the elbow method rather than alone. The silhouette coefficient is the most popular. For a point i, let a be its mean distance to the other points in its own cluster and b its mean distance to the points of the nearest other cluster. Then s = (b − a) / max(a, b). The value lies between −1 and +1: near +1 means the point is well inside its cluster, near 0 means it is on a boundary and negative means it is probably in the wrong cluster. The mean over all points is the silhouette score. The Davies-Bouldin index compares the spread inside each cluster with the distance between cluster centres; lower is better.
External Measures
When true labels exist, for testing an algorithm, external measures compare the clusters with them. The Rand index counts the pairs of points on which the two groupings agree, and the adjusted Rand index (ARI) corrects for chance: it is 1 for a perfect match and about 0 for a random one. Purity and normalised mutual information are other examples.
Using the Measures
No measure is perfect. Use several, plot the clusters when the data has two or three dimensions, and ask whether the clusters are meaningful for the application, since a mathematically good clustering may be useless for the business question.
Definition: Silhouette coefficient. For a point, (b − a) / max(a, b), where a is its mean distance to its own cluster and b its mean distance to the nearest other cluster; values near +1 indicate good clustering.
Table 4.5: Clustering validation measures
| Measure | Kind | Better when | Range |
|---|---|---|---|
| Inertia (SSE) | Internal | Lower | 0 upwards |
| Silhouette score | Internal | Higher | −1 to +1 |
| Davies-Bouldin | Internal | Lower | 0 upwards |
| Adjusted Rand index | External | Higher | About 0 to 1 |
Example 4.5: Silhouette for two clusters
Problem: Clusters are {2, 3, 4} and {10, 11, 12}. Find the silhouette of the points 2 and 3, and the mean silhouette of all six points.
Solution:
- For the point 2: a = (1 + 2) / 2 = 1.5 and b = (8 + 9 + 10) / 3 = 9, so s = (9 − 1.5) / 9 = 0.833.
- For the point 3: a = (1 + 1) / 2 = 1 and b = (7 + 8 + 9) / 3 = 8, so s = 7 / 8 = 0.875.
- For the point 4: a = 1.5, b = (6 + 7 + 8) / 3 = 7, so s = 0.786.
- The second cluster is the mirror image: 0.833, 0.875 and 0.786. The mean is (0.833 + 0.875 + 0.786) / 3 = 0.831.
Result: The silhouette score is 0.831, showing a clear, well-separated clustering.
import numpy as np
from sklearn.cluster import KMeans
from sklearn.metrics import (silhouette_score, davies_bouldin_score,
adjusted_rand_score)
D = np.array([[2], [3], [4], [10], [11], [12]])
km = KMeans(n_clusters=2, init=np.array([[2], [12]]), n_init=1)
lab = km.fit_predict(D)
print("silhouette :", round(silhouette_score(D, lab), 3))
print("Davies-Bouldin:", round(davies_bouldin_score(D, lab), 3))
true = [0, 0, 0, 1, 1, 1]
print("adjusted Rand :", adjusted_rand_score(true, lab))
Output
silhouette : 0.831
Davies-Bouldin: 0.167
adjusted Rand : 1.0
Common mistakes
- Choosing k by minimum inertia alone.
- Reading a silhouette of 0 as good; it means overlapping clusters.
- Treating a lower Davies-Bouldin value as worse.
- Judging clusters by the numbers without checking that they make sense.
Key takeaways
- Silhouette s = (b − a) / max(a, b), from −1 to +1; higher is better.
- Davies-Bouldin: lower is better.
- ARI compares clusters with true labels and is about 1 for a perfect match.
- Inertia falls with k, so use the elbow, not the minimum.
Practice questions
- What do a and b mean in the silhouette coefficient?
- Is a higher or lower Davies-Bouldin index better?
- When can external measures be used?
Answers to the practice questions
- a is the mean distance within the point's cluster; b is the mean distance to the nearest other cluster.
- Lower.
- When true labels are available for comparison.
Sources
- Official BCA syllabus Introduction to Machine Learning, course code 26BCA404DSE21, Unit IV
