0

Background

Clustering is a technique in unsupervised learning used to group similar data points. There are multiple clustering algorithms (ex. K-Means, Hierarchical Clustering, DBSCAN, Gaussian Mixture Models) and each has its own way of grouping data points. Different clustering metrics, each measuring different things, are used to evaluate and quantify the goodness of a clustering. 

The two main categories of clustering metrics are:

  1. External measures: compare your cluster results against a ground truth that behaves like a “correct answer” that you already know. To evaluate the “goodness” of the clustering, you measure how close your clustering is to the known correct clustering. 
  2. Internal measures: judge the quality of the clustering solely based on the clustering results themselves, without needing any outside information. To evaluate the “goodness” of the clustering, you look at questions like, “Are items within each cluster similar to each other?”, and “Are the different groups clearly distinct from each other?”

Silhouette score is an internal evaluation metric for clustering algorithms, introduced by Peter J. Rousseeuw in the 1987 paper “Silhouettes: A Graphical Aid to the Interpretation and Validation of Cluster Analysis.”

A “good” clustering would be one in which points within a cluster are similar to each other, and points from different clusters are dissimilar. Such a clustering would be one where points of different clusters are geometrically separable. Silhouette score quantifies this with how well each data point fits within its assigned cluster compared to other clusters, capturing both cohesion (within-cluster similarity) and separation (between-cluster dissimilarity). 

Definition

Title: Visualization of the different terms in silhouette score definition

The Silhouette Coefficient of a point is defined as 

where is the mean distance from to points in its own cluster :

1. Mean Internal Distance ()

This value tells us how “crowded” or “comfortable” a point is within its own group by calculating the mean distance from to points in its own cluster :

  • (Mean “In”): The average distance to points inside its own cluster (shown as grey arrows in the above figure).  
  • The Symbol (Sigma): This simply means “add them all up.” Here, we are adding up the distances between our point and every other point .
  • : This translates to “every point that is in Cluster .”
  • : a distance function that gives the distance (usually Euclidean or straight-line distance) between the two points
  • The Denominator (): We divide by the total number of points in the cluster minus one. We subtract one because we aren’t measuring the distance from the point to itself.

2. Mean External Distance ()

This value looks at the clusters the point doesn’t belong to and finds the closest neighbor by calculating the mean distances from to points in the closest cluster: 

  • (Mean “Out”): The average distance to points in a cluster outside its own (shown as red and purple arrows).
  • : This tells us to calculate the average distance to every other cluster and then pick the smallest one (the minimum).
  • Why find the minimum? We want to know how close our point is to being “misclassified” into the nearest neighboring group.

The silhouette score for a clustering is the average of the scores for each individual point:

Silhouette score takes on values from -1 to 1, with a silhouette score close to 1 implying a good clustering (points are close to their own clusters but far from other clusters). A value of 0 indicates that the data point is at or near the decision boundary between two neighboring clusters, meaning it is ambiguous which cluster it belongs to. A negative value indicates that the data point might have been assigned to the wrong cluster. 

Code Example

from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt
 
# Generate sample data
X, _ = make_blobs(n_samples=500, centers=4, random_state=42)
  
# Test different k values
silhouette_scores = []
K_range = range(2, 11)
 
for k in K_range:
    kmeans = KMeans(n_clusters=k, random_state=42)
    labels = kmeans.fit_predict(X)
    score = silhouette_score(X, labels)
    silhouette_scores.append(score)
    print(f"k={k}: Silhouette Score = {score:.3f}")
 
  
 
# Plot silhouette scores
plt.figure(figsize=(10, 6))
plt.plot(K_range, silhouette_scores, 'bo-')
plt.xlabel('Number of Clusters (k)')
plt.ylabel('Silhouette Score')
plt.title('Silhouette Score vs Number of Clusters')
plt.grid(True)
plt.show()
 
# Find optimal k
optimal_k = K_range[silhouette_scores.index(max(silhouette_scores))]
print(f"\nOptimal number of clusters: {optimal_k}")

Output:
k=2: Silhouette Score = 0.596
k=3: Silhouette Score = 0.761
k=4: Silhouette Score = 0.791
k=5: Silhouette Score = 0.688
k=6: Silhouette Score = 0.560
k=7: Silhouette Score = 0.471
k=8: Silhouette Score = 0.443
k=9: Silhouette Score = 0.429
k=10: Silhouette Score = 0.345

Title: Graph of silhouette score vs. number of clusters

Optimal number of clusters: 4

Analysis:
The starting dataset is randomly generated with 4 centers, like such:

Title: Original dataset used for the above code example

The highest silhouette score indicates the optimal number of clusters. In this example, you’ll see that k=4 produces the best score, which aligns with the true number of clusters in our generated data. 

Title: Resulting clustering visualization for different k values

You can see how too low and too high of a k results in lower silhouette scores, and suboptimal clustering.

Diving Deeper

Why use silhouette score?

Since silhouette score is an internal measure of clustering, it does not require ground-truth labels, meaning you do not need a label specifying which cluster a data point belongs to. This is valuable because in unsupervised learning scenarios, true labels are often unavailable or expensive to obtain, making external validation metrics impractical. 

Silhouette score is also useful for choosing the number of clusters k in an algorithm like K-means clustering. By providing both an aggregated numerical score and per-point diagnostics (through individual silhouette values that indicate whether points are well-clustered, ambiguous, or potentially misassigned), silhouette score offers a comprehensive view of clustering quality. 

Unlike the elbow method which can be subjective and sometimes lacks a clear “elbow” point, silhouette score provides an objective measure that captures both cohesion and separation simultaneously. You can compute silhouette scores for different values of and select the that maximizes the score, indicating the most natural grouping of the data.

Limitations

Silhouette score is inherently distance-dependent, meaning its value is sensitive to the choice of distance metric (e.g. Euclidean, cosine, or Manhattan). In addition, because clustering algorithms like k-means rely on random initialization, the resulting cluster assignments and thus the silhouette score may vary across runs even on the same dataset. This can potentially lead to different conclusions about the optimal number of clusters.

It can also be computationally expensive for very large datasets, since computing the necessary pairwise distances has time complexity, which becomes a problem as the number of points grows. 

Additionally, the silhouette score implicitly assumes convex and well-separated clusters, so it may be misleading for data with complex structures (such as concentric circles, moon-shaped clusters, or significant overlap), often favoring spherical and compact clusters. A compact cluster is one where the data points are tightly packed together with very little space between them, indicating high similarity among all members of that group.

The metric also tends to favor smaller values of over larger values (so favors fewer clusters), as increasing the number of clusters can assign points to less optimal clusters and reduce the average silhouette score even when finer-grained groupings are meaningful.

Finally, relying on a single global silhouette score can mask local issues in the clustering, since individual points may have very different silhouette values; examining per-point or per-cluster silhouettes is often necessary to uncover more nuanced problems.

Comparison with other clustering metrics

While the silhouette score is widely used because it captures both cluster cohesion and separation in a single measure, it is often most informative when compared with other evaluation metrics. 

  • The Elbow Method (WCSS) considers only within-cluster variance and ignores how well clusters are separated, making it more of a heuristic than a true quality metric. 
  • The Calinski–Harabasz Index measures the ratio of between-cluster variance to within-cluster variance, with higher values corresponding to more distinct clusters. 
  • The Dunn Index focuses on the ratio of the minimum inter-cluster distance to the maximum intra-cluster distance, emphasizing well-separated and compact clusters. 

In practice, using multiple metrics together provides a more robust and reliable assessment of clustering quality than relying on any single score.

Videos

Related Articles

References

Rousseeuw, P. J. (1987). Silhouettes: A Graphical Aid to the Interpretation and Validation of Cluster Analysis. Journal of Computational and Applied Mathematics, 20, 53-65. 

scikit-learn documentation: sklearn.metrics.silhouette_score 

Kaufman, L., & Rousseeuw, P. J. (1990). Finding Groups in Data: An Introduction to Cluster Analysis. Wiley.