K-means (Lloyd's algorithm) repeats two steps until the centroids stop moving: assign every point to its nearest centroid, then move each centroid to the mean of the points assigned to it. That process always finds some K clusters — but it never tells you whether K was the right choice.
The silhouette coefficient answers that question, per point:
a(i) = mean distance from i to other points
in its own cluster (cohesion)
b(i) = mean distance from i to the points
of its nearest other cluster (separation)
s(i) = ( b(i) − a(i) ) / max( a(i), b(i) )
s(i) sits in [-1, 1]. Near +1 means the point is far tighter to its own cluster than to any rival — a good assignment. Near 0 means it sits on a boundary. Negative means it is actually closer to another cluster than its own — likely mis-clustered. Averaging s(i) over every point gives one score per K.
- K slider — sets the number of clusters K-means is asked to find; try one too small and one too large and watch the bars.
- Spread — widens or tightens the Gaussian blobs the dataset is drawn from, controlling how separable the true groups are.
- New Dataset — redraws a fresh mixture of 3–6 hidden Gaussian blobs.
- Run K-Means — animates Lloyd's algorithm converging for the current K; bar height = |s(i)|, bar color green→red as s(i) falls from +1 to negative.
- Scan K = 2…8 — runs K-means to convergence for every K and plots the average silhouette per K. Unlike inertia (which always keeps dropping as K rises — the flaw the "elbow method" struggles with), average silhouette peaks near the true number of groups, then falls again once you over-split real clusters.
Real-world relevance: this is exactly how data scientists pick K for customer segmentation, document clustering, or anomaly-cluster discovery when the "true" group count is unknown — scikit-learn's silhouette_score implements precisely this formula.