The Silhouette Score for Cluster Quality

The Silhouette Score measures how similar each sample is to its own cluster compared to neighboring clusters, providing an objective measure of clustering quality from -1 (wrong cluster) to +1 (perfect cluster).


Silhouette Score Formula

For sample i: s(i) = (b(i) - a(i)) / max(a(i), b(i)), where a(i) is the mean intra-cluster distance and b(i) is the mean distance to the nearest other cluster.

Interpreting the Score

  • s \u2248 1: Sample is well inside its cluster and far from neighboring clusters.
  • s \u2248 0: Sample is near a cluster boundary.
  • s \u2248 -1: Sample may be assigned to the wrong cluster.

The overall Silhouette Score is the mean over all samples.

Computing Silhouette Score in sklearn

Use sklearn.metrics.silhouette_score for the global average or silhouette_samples for per-sample scores.

Selecting K with Silhouette

<pre><code class="language-python">from sklearn.cluster import KMeans from sklearn.metrics import silhouette_score from sklearn.datasets import make_blobs import matplotlib.pyplot as plt X, _ = make_blobs(n_samples=500, centers=4, cluster_std=0.9, random_state=42) scores = [] for k in range(2, 11): km = KMeans(n_clusters=k, init='k-means++', n_init=10, random_state=42) labels = km.fit_predict(X) scores.append(silhouette_score(X, labels)) plt.plot(range(2, 11), scores, 'go-') plt.xlabel('Number of Clusters K') plt.ylabel('Silhouette Score') plt.title('Silhouette Score vs. K') plt.show() best_k = range(2, 11)[scores.index(max(scores))] print(f"Best K: {best_k}")</pre>

Silhouette Diagrams

<pre><code class="language-python">from sklearn.metrics import silhouette_samples import numpy as np km = KMeans(n_clusters=4, random_state=42, n_init=10).fit(X) labels = km.labels_ sample_scores = silhouette_samples(X, labels) # Per-cluster average silhouette for c in range(4): cluster_scores = sample_scores[labels == c] print(f"Cluster {c}: mean silhouette = {cluster_scores.mean():.3f}, n = {len(cluster_scores)}")</pre>

Silhouette vs. Inertia

Unlike inertia, the Silhouette Score penalizes clusters that are not well-separated, making it a more holistic quality measure. It works across different clustering algorithms, not just K-Means.

Algorithm-Agnostic Application

The Silhouette Score can be applied to any clustering algorithm that produces cluster labels — including DBSCAN, hierarchical clustering, and GMMs — making it the most versatile cluster quality metric in scikit-learn.