Select a cluster count
Compare candidate clusterings produced with different numbers of clusters.
Scikit-Learn · Function
Computes the mean Silhouette Coefficient for a clustering assignment. Scores near 1 indicate well-separated clusters, near 0 indicate overlap, and negative values often indicate unsuitable assignments.
For each sample, the function compares its mean distance to points in its own cluster with the mean distance to the nearest other cluster, using (b - a) / max(a, b). It returns the average of those sample-level coefficients. Higher values indicate denser, better-separated clusters; values near zero indicate overlap and negative values usually suggest incorrect assignments.
sklearn.metrics.silhouette_score(X, labels, *, metric='euclidean', sample_size=None, random_state=None, **kwds)| Name | Type | Description |
|---|---|---|
X | array-like or sparse matrix of shape (n_samples, n_features), or (n_samples, n_samples) | Feature matrix or pairwise distance matrix, depending on metric. |
labels | array-like of shape (n_samples,) | Cluster labels assigned to each sample. |
metric | str or callable, default='euclidean' | Distance metric used for pairwise distances; defaults to Euclidean distance. |
sample_size | int, default=None | Number of samples to use when computing the score. If None, use all samples. |
random_state | int, RandomState instance or None, default=None | Controls sampling when sample_size is set. |
**kwds | dict | Additional keyword arguments passed to the distance function. |
constraints | — | The score is defined only when the number of labels is between 2 and n_samples - 1. |
float: Mean Silhouette Coefficient for all samples.
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
X, _ = make_blobs(n_samples=30, centers=3, random_state=0)
labels = KMeans(n_clusters=3, n_init="auto", random_state=0).fit_predict(X)
score = silhouette_score(X, labels)
print(f"Silhouette score: {score:.3f}")Clusters synthetic points, then computes the mean Silhouette Coefficient for the resulting assignments.
Compare candidate clusterings produced with different numbers of clusters.
Assess clustering when ground-truth labels are unavailable.
Evaluate assignments using a custom distance function or precomputed distances.
It returns the mean Silhouette Coefficient across all samples. Each coefficient compares mean intra-cluster distance a with mean nearest-cluster distance b using (b - a) / max(a, b).
Follow-up: What does a negative value suggest? It generally means the sample may fit another cluster better than its assigned cluster.
The score ranges from -1 to 1. Values near 1 indicate well-separated clusters, values near zero indicate overlap, and negative values generally indicate potentially incorrect assignments.
Follow-up: Can a high score prove the clustering is correct? No; it measures geometric separation, not agreement with unknown ground-truth labels.
Use silhouette_score for a single aggregate value, and silhouette_samples when per-sample coefficients are needed. The function accepts a distance metric and forwards additional metric keyword arguments through kwds.
Follow-up: When is sample_size useful? It can reduce computation by scoring a random subset, with random_state controlling reproducibility.
silhouette_samplespairwise_distancescalinski_harabasz_scoredavies_bouldin_score