Skip to content

Evaluation, diagnostics, benchmarks, and additional tests · Evaluation, diagnostics, benchmarks, and additional hypothesis tests · EVA001 · Metric

Silhouette coefficient

Measures whether each point is closer to its assigned cluster than to its nearest competing cluster.

Runs in CodeSplash Complete calculation CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Complete calculation
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through discover.choose_k, discover.stability. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Measures whether each point is closer to its assigned cluster than to its nearest competing cluster.

Question / hypotheses

How separated and cohesive is this partition under the chosen distance? This is a descriptive score, not a hypothesis test.

Architecture / computational structure

A fixed scoring/calibration pipeline applied to model outputs or supplied observations; the formula and usage sections identify its operations.

Assumptions

Distances must encode useful similarity. At least two clusters and fewer clusters than observations are required. Decide how noise labels and singleton clusters are handled.

Formula / computational definition

si=(bi−ai)/max⁡(ai,bi),S=n−1∑isi.s_i=(b_i-a_i)/\max(a_i,b_i),\qquad S=n^{-1}\sum_i s_i.

Parameters and notation

a_i is mean within-cluster distance excluding the point; b_i is the smallest mean distance to another cluster. Hyperparameters are distance, sampling size, and candidate partitions; the score has no learned weights.

Fitting, fine-tuning, and adaptation

There is no model fine-tuning step for this evaluation procedure. Freeze the evaluated systems and evaluator before the final test. Select evaluation settings on development data; if a learned evaluator or calibration model is adapted, use separate fitting data, version it, and reassess its validity.

How to use and calibrate

Compare a prespecified range of cluster counts using the same preprocessing and distance. Inspect the distribution by cluster, not only its overall mean. Use repeated fits and resampling to assess stability.

Example application

Compare k-means with k=2,3,4 on standardized customer features and plot per-customer silhouettes.

Interpretation / pitfalls

Watch out

High values favor separated compact clusters and need not favor a useful density partition. Selecting the largest score is not evidence that a true number of clusters exists. A silhouette in a two-dimensional embedding describes that embedding.

Python and R code examples

Scope: Complete silhouette calculation for a small supplied partition. Python: scikit-learn; R: cluster.

Validation: Python — Executed successfully on synthetic inputs. R — reviewed but not executed in this environment. Python dependencies are explicit imports; R uses base R unless a package/bridge is explicitly named.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Complete calculation — Complete silhouette calculation for a small supplied partition. Python: scikit-learn; R: cluster. · needs numpy, sklearn

import numpy as np
from sklearn.metrics import silhouette_score
X=np.array([[0.,0.],[0.,1.],[1.,0.],[8.,8.],[8.,9.],[9.,8.]])
print(silhouette_score(X,[0,0,0,1,1,1]))
R

Complete calculation — Complete silhouette calculation for a small supplied partition. Python: scikit-learn; R: cluster. · needs cluster

X <- rbind(c(0,0),c(0,1),c(1,0),c(8,8),c(8,9),c(9,8))
print(mean(cluster::silhouette(c(1,1,1,2,2,2),dist(X))[,3]))

Sources

From the CodeSplash reference, entry EVA001.