Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Function

silhouette_score

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.

How it works

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.

Syntax

sklearn.metrics.silhouette_score(X, labels, *, metric='euclidean', sample_size=None, random_state=None, **kwds)

Parameters

NameTypeDescription
Xarray-like or sparse matrix of shape (n_samples, n_features), or (n_samples, n_samples)Feature matrix or pairwise distance matrix, depending on metric.
labelsarray-like of shape (n_samples,)Cluster labels assigned to each sample.
metricstr or callable, default='euclidean'Distance metric used for pairwise distances; defaults to Euclidean distance.
sample_sizeint, default=NoneNumber of samples to use when computing the score. If None, use all samples.
random_stateint, RandomState instance or None, default=NoneControls sampling when sample_size is set.
**kwdsdictAdditional 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.

Returns

float: Mean Silhouette Coefficient for all samples.

Key points

  • The function produces one aggregate clustering-quality score.
  • Each sample is evaluated using its within-cluster distance and distance to the closest alternative cluster.
  • The sample-level coefficient is normalized by the larger of the two distance terms.
  • The metric is bounded from -1 to 1, with larger values generally preferable.
  • A result close to zero signals weak separation between groups.
  • Negative results often indicate that assignments are less compatible with the observed distances.

Example: Evaluate K-means clustering

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.

When to use it

Select a cluster count

Compare candidate clusterings produced with different numbers of clusters.

Unsupervised evaluation

Assess clustering when ground-truth labels are unavailable.

Distance-aware validation

Evaluate assignments using a custom distance function or precomputed distances.

Interview questions

QWhat does silhouette_score measure?

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.

QHow should the returned value be interpreted?

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.

QHow do you obtain individual sample scores or customize distances?

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_samples
  • pairwise_distances
  • calinski_harabasz_score
  • davies_bouldin_score

Source: silhouette_score — scikit-learn 1.9.1 documentation. Checked 2026-10-15.