Start learning

Silhouette Score

Lesson 44. About 6 minutes.

The silhouette score measures how well each point fits its own cluster compared with the nearest other cluster. It ranges from −1 to 1, and higher is better.

Your Path Through This Lesson

0 of 5 done

By the End of This Lesson You Can
  • Compute a point's silhouette value by hand.
  • Use the average silhouette score to compare clusterings.
  • Use the silhouette score to help choose K.

Judging Clusters Without Labels

With supervised models we compare predictions to the true labels. In clustering there are no labels, so we need a different question: are points close to their own cluster and far from the others?

The Formula

For each point :

  • = average distance to the other points in its own cluster (how tight its cluster is).
  • = average distance to the points in the nearest other cluster (how far the neighbours are).
Meaning
Close to 1 Well inside its own cluster, far from others
Around 0 On the border between two clusters
Negative Probably in the wrong cluster: it's closer to another one

The silhouette score of a clustering is the average over all points.

By Hand

Study hours from the K-means lesson: 1, 2, 3 in cluster A and 10, 11, 12 in cluster B.

Point 1 (in A):

Point 2 (in A):

Point 3 is the mirror image of point 1 on the near side: , , . Cluster B is symmetrical, so the overall average is about 0.85: a clean clustering.

python
import numpy as np
from sklearn.metrics import silhouette_samples, silhouette_score

hours = np.array([[1], [2], [3], [10], [11], [12]])
labels = np.array([0, 0, 0, 1, 1, 1])

print("each point:", silhouette_samples(hours, labels).round(3))
print(f"average   : {silhouette_score(hours, labels):.3f}")

bad = np.array([0, 0, 1, 1, 0, 1])      # a deliberately poor grouping
print(f"bad grouping average: {silhouette_score(hours, bad):.3f}")
Output
each point: [0.85  0.889 0.812 0.812 0.889 0.85 ]
average   : 0.850
bad grouping average: -0.038

A poor grouping scores far lower, with some points negative.

Choosing K

Unlike inertia, the silhouette score doesn't automatically improve with more clusters. Splitting a real cluster in two makes points close to the "other" half, which lowers . So the K with the highest score is a strong candidate.

python
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs

X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.9, random_state=1)

for k in range(2, 8):
    labels = KMeans(n_clusters=k, n_init=10, random_state=0).fit_predict(X)
    print(f"K={k}: silhouette {silhouette_score(X, labels):.3f}")
Output
K=2: silhouette 0.715
K=3: silhouette 0.606
K=4: silhouette 0.661
K=5: silhouette 0.576
K=6: silhouette 0.519
K=7: silhouette 0.399

We generated 4 groups, yet the highest score is at K = 2. Look at the centroids from the K-means lesson: three of the four blobs sit close together on one side, and one sits far away. At a coarse level, "far blob vs the rest" really is the cleanest split. K = 4 is a clear second peak, and it's where the elbow pointed.

The lesson: no single score settles K. Check the elbow, the silhouette and, if you can, a plot of the clusters, then choose the K that makes sense for your problem.

Note

The silhouette needs at least 2 clusters, and it costs distance computations. On large datasets, pass sample_size= to silhouette_score to estimate it from a random subset.

Watch Out

The silhouette favours compact, round clusters. For long or oddly shaped clusters (where DBSCAN shines), it can rate a good clustering poorly. Treat it as a guide, not a verdict.

Try It Yourself
  1. Compute by hand for point 10 in the example. Does it match the output?
  2. Give Noman's 1 hour to cluster B in labels and recompute. What happens to point 1's value?
The Key Idea

Silhouette : tight own cluster (small a) and distant neighbours (large b) give a value near 1. Prefer a K with a high average.

Summary

  • = average distance within the own cluster; = to the nearest other cluster.
  • , from −1 (wrong cluster) to 1 (perfect).
  • Average over points to score a clustering; a high average marks a good K, but check it against the elbow and a plot.
  • Next: a clustering method that finds any shape and flags outliers, DBSCAN.