Sub topic 15
Evaluating clusterings without ground truth
This chapter assumes you can run AgglomerativeClustering and DBSCAN from the earlier chapters and know what a distance matrix is. Real clustering data rarely comes with true labels, so you need scores computed from the data and the labels alone. These are called internal metrics.
The silhouette score
For each point, the silhouette compares two numbers. The value a is the mean distance to the other points in its own cluster. The value b is the mean distance to the points in the nearest other cluster. The point's silhouette is (b - a) / max(a, b), which lies between -1 and 1.
- Near 1: the point sits well inside its cluster.
- Near 0: the point is on a boundary between clusters.
- Negative: the point is probably closer to another cluster.
silhouette_score returns the mean over all points. It needs at least 2 clusters and at most n_samples - 1.
from sklearn.cluster import AgglomerativeClustering
from sklearn.datasets import make_blobs
from sklearn.metrics import silhouette_score
X, _ = make_blobs(n_samples=300, centers=3, cluster_std=0.8, random_state=1)
for k in range(2, 7):
labels = AgglomerativeClustering(n_clusters=k, linkage="ward").fit_predict(X)
print(k, round(silhouette_score(X, labels), 3))The k with the highest score is a reasonable candidate. Treat it as a hint, not a verdict.
Other internal scores
| Metric | Better when | Notes |
|---|---|---|
| Silhouette | Higher | Needs pairwise distances, so cost grows with the square of the sample count |
| Calinski-Harabasz | Higher | Ratio of between-cluster to within-cluster spread; cheap to compute |
| Davies-Bouldin | Lower | Average similarity of each cluster to its closest neighbor |
Why density-based results need care
All three metrics reward compact, well-separated, roughly convex clusters. DBSCAN finds clusters of arbitrary shape, so a correct clustering of two interleaved half-moons can score worse than a poor k-means split of the same data.
Noise is the second problem. DBSCAN labels outliers -1. If you pass those labels to a metric, the noise points are treated as one extra cluster, which distorts the score. Exclude them, as in the earlier DBSCAN example, and report the fraction of noise separately. A clustering that discards 60% of the data can score well on the rest.
import numpy as np
from sklearn.cluster import DBSCAN
from sklearn.datasets import make_blobs
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import StandardScaler
X, _ = make_blobs(n_samples=300, centers=3, cluster_std=0.6, random_state=7)
X = StandardScaler().fit_transform(X)
for eps in [0.1, 0.2, 0.3, 0.5]:
labels = DBSCAN(eps=eps, min_samples=5).fit_predict(X)
mask = labels != -1
n_clusters = len(set(labels[mask]))
noise = 1 - mask.mean()
if n_clusters >= 2:
score = round(silhouette_score(X[mask], labels[mask]), 3)
else:
score = None
print(eps, n_clusters, f"noise={noise:.2f}", score)Other checks
- Plot the clusters, or a 2-D projection of them. No score replaces looking.
- Re-run with different samples or parameters. Clusters that change a lot are unstable.
- Ask whether the clusters mean something for your task, such as customer segments that a domain expert can describe.
Exercise
DBSCAN on your data returns 3 clusters and a silhouette score of 0.71 on the non-noise points, but 55% of points are labeled -1. Is this a good clustering?
Show answer
Not necessarily. The score describes only the 45% of points that were kept, and those may be the easy, dense ones. Report the noise fraction with the score, and try a larger eps or a smaller min_samples to see whether more points can be assigned without the score collapsing.
Exercise
Why can a correct DBSCAN clustering of two interleaved half-moons get a lower silhouette score than a k-means split of the same data into two halves?
Show answer
Silhouette favors compact, well-separated clusters. Points in a half-moon can be closer to the other moon than to the far end of their own, so the correct clusters have a large a and a small b. A straight k-means split produces more compact groups and scores higher, even though it cuts the moons apart.