The silhouette score, also known as the silhouette coefficient, is a metric used in cluster analysis to evaluate the quality of a clustering solution. For each data point, it measures how similar that point is to its own cluster compared to other clusters. The score ranges from –1 to 1, where a high value indicates that the point is well matched to its own cluster and poorly matched to neighboring clusters, and a low or negative value suggests potential misassignment. The mean silhouette score over all points provides an overall assessment of cluster separation and cohesion, and is widely employed for tasks such as selecting the optimal number of clusters.

1 Definition and Formula

1.1 Mathematical Definition

1.1.1 Intra‑cluster distance (a)

For a given data point \(i\) belonging to cluster \(C_I\), the intra‑cluster distance \(a(i)\) is the average distance between \(i\) and all other points in the same cluster. If the cluster contains only one point, \(a(i)\) is defined as 0. The distance metric (e.g., Euclidean, Manhattan) is chosen before computation.

\[

a(i) = \frac{1}{C_I- 1} \sum_{j \in C_I, j \neq i} d(i,j)

\]

1.1.2 Nearest‑cluster distance (b)

For the same point \(i\), the nearest‑cluster distance \(b(i)\) is the smallest average distance from \(i\) to all points in any other cluster (the “neighboring cluster”). For each cluster \(C_K\) that does not contain \(i\), the mean distance from \(i\) to points in \(C_K\) is computed; \(b(i)\) is the minimum of these means.

\[

b(i) = \min_{K \neq I} \frac{1}{C_K} \sum_{j \in C_K} d(i,j)

\]

1.1.3 Silhouette value for a single point

The silhouette value \(s(i)\) for point \(i\) is defined as:

\[ s(i) = \frac{b(i) - a(i)}{\max\{a(i), b(i)\}} \]

When \(a(i) < b(i)\), the numerator is positive and \(s(i)\) lies in (0, 1]; when \(a(i) > b(i)\), the numerator is negative and \(s(i)\) lies in [–1, 0); when \(a(i) = b(i)\), \(s(i) = 0\). If a cluster has only one point, \(s(i)\) is defined as 0 by convention.

1.2 Interpretation of Values

1.2.1 Positive values

A positive silhouette value indicates that the point is closer to its own cluster than to its neighboring cluster. Values near +1 imply that the point is compactly placed inside its cluster and well separated from other clusters. The higher the positive value, the better the assignment.

1.2.2 Negative values

A negative silhouette value suggests that the point lies, on average, closer to points in a different cluster than to its own cluster. This is a sign of potential misassignment; the point might be more appropriately placed in the neighboring cluster. Values near –1 indicate strong misclassification.

1.2.3 Zero values

A silhouette value of zero (or near zero) indicates that the point is approximately equidistant between two clusters. Its assignment is ambiguous, and the clustering outcome does not clearly separate it from the neighboring cluster.

2 Computation

2.1 Step‑by‑step procedure

  1. Perform clustering on the dataset, obtaining a set of clusters and cluster labels for each point.
  2. Choose a distance metric (e.g., Euclidean distance).
  3. For each point \(i\):
  • Compute \(a(i)\) as the average distance to all other points in the same cluster.
  • Compute \(b(i)\) as the minimum average distance to points in any other cluster.
  • Calculate \(s(i)\) using the formula above.
  1. Optionally, average \(s(i)\) over all points to obtain the mean silhouette score.

2.2 Time complexity

Computing the silhouette score has a time complexity of \(O(n^2)\), where \(n\) is the number of data points. For each point, distances to all other points must be computed, leading to a pairwise distance calculation. This quadratic complexity makes the metric computationally expensive for very large datasets.

2.3 Variants

2.3.1 Silhouette score for a cluster

For a specific cluster \(C\), the cluster silhouette score is the average of \(s(i)\) over all points in that cluster. It gives a local measure of how well‑formed that cluster is.

2.3.2 Mean silhouette score

The mean silhouette score is the arithmetic mean of \(s(i)\) over all points in the dataset. It provides a single global measure of clustering quality, often used to compare different clusterings or to select the number of clusters.

3 Properties

3.1 Bounded nature

The silhouette value for any point is always in the interval [–1, 1]. This bounded nature allows easy interpretation: values near 1 indicate good clustering, values near –1 indicate poor assignment, and values near 0 indicate borderline cases.

3.2 Scale invariance

The silhouette score depends only on the relative distances between points, not on the absolute scale of the data. If all distances are multiplied by a constant factor, the ratios \(a(i)\) and \(b(i)\) change equally, leaving \(s(i)\) unchanged. This makes the score invariant to linear scaling of the features.

3.3 Relationship to other metrics

3.3.1 Compared to Davies–Bouldin index

Both the silhouette score and the Davies–Bouldin index are internal clustering validation metrics. The Davies–Bouldin index measures the average similarity between each cluster and its most similar cluster; lower values indicate better separation. The silhouette score, in contrast, operates at the point level and expresses separation in a bounded, interpretable range.

3.3.1.1 Advantages over Davies–Bouldin
  • The silhouette score provides per‑point information, enabling detection of outliers or ambiguous points, while the Davies–Bouldin index only gives a cluster‑level summary.
  • The silhouette score is bounded between –1 and 1, making it easier to interpret across different datasets and numbers of clusters. The Davies–Bouldin index has no fixed upper bound.
  • The silhouette score is directly based on distances to both the own cluster and the nearest neighboring cluster, offering a more intuitive measure of cohesion and separation.

3.3.2 Compared to Dunn index

The Dunn index is defined as the ratio of the minimum inter‑cluster distance to the maximum intra‑cluster distance. It focuses on the worst‑case separation and cohesion. The silhouette score takes an average perspective and considers all points rather than extremes. The Dunn index tends to be more sensitive to outliers and may be less stable for large datasets, whereas the silhouette score provides a more robust overall assessment.

4 Applications

4.1 Determining optimal number of clusters

The mean silhouette score is widely used to select the number of clusters in a dataset. The standard approach is to run a clustering algorithm (e.g., k‑means) with different values of \(k\) and compute the mean silhouette score for each clustering. The value of \(k\) that yields the highest mean silhouette score is considered the optimal number of clusters, as it maximizes the average separation and cohesion.

4.2 Outlier detection

Points with very low or negative silhouette scores are likely outliers or points that do not fit well into any cluster. By ranking points by their silhouette values, analysts can identify and examine such points for potential data quality issues or unusual characteristics.

4.3 Validation of clustering algorithms

The silhouette score is used as an internal validation metric to compare the performance of different clustering algorithms on the same dataset. For example, one might compare k‑means, hierarchical clustering, and DBSCAN by computing the mean silhouette score of each result. The algorithm that produces the highest score is considered to have produced the most appropriate clustering structure, given the assumptions of the metric.

5 Limitations

5.1 Computational cost for large datasets

The \(O(n^2)\) time complexity of computing all pairwise distances makes the silhouette score impractical for very large datasets (e.g., millions of points). Approximations, such as using a random subsample or a representative subset of points, are sometimes employed, but these reduce the reliability of the score.

5.2 Sensitivity to distance metric

The silhouette score depends heavily on the choice of distance metric. Using Euclidean distance assumes spherical clusters, while other metrics (e.g., Manhattan) may produce different results. The score may not reflect cluster validity if the chosen metric does not align with the underlying structure of the data.

5.3 Assumption of convex clusters

The silhouette score implicitly assumes that clusters are convex and of similar density. For non‑convex clusters (e.g., crescent‑shaped or elongated clusters), the nearest‑cluster distance \(b(i)\) may be misleading, as points at the boundaries of a convex shape may be closer to another cluster than to points far away in the same cluster. In such cases, the silhouette score tends to be low even if the clustering is meaningful for non‑convex shapes.

6 Software Implementations

6.1 Python (scikit‑learn)

The silhouette_score function in the sklearn.metrics module computes the mean silhouette score for a given dataset and cluster labels. The silhouette_samples function returns the silhouette value for each individual point. Both functions accept a distance metric (default is Euclidean).

from sklearn.metrics import silhouette_score, silhouette_samples
score = silhouette_score(X, labels)
samples = silhouette_samples(X, labels)

6.2 R (cluster package)

The silhouette function in the cluster package computes silhouette information for a clustering result. It returns an object of class silhouette that contains individual silhouette widths and the average silhouette width for each cluster and overall.

library(cluster)
sil <- silhouette(clustering_result$cluster, dist(X))
summary(sil)

6.3 MATLAB

MATLAB provides the evalclusters function with the criterion 'Silhouette' to evaluate clustering solutions. The silhouette function can also be used to compute silhouette values for a given clustering.

eva = evalclusters(X, 'kmeans', 'Silhouette', 'KList', 1:10);
plot(eva)
sil_values = silhouette(X, idx);