Machine Learning · Insper

Clustering

Groups without labels, and the numbers that judge them

k-means, DBSCAN (density-based spatial clustering of applications with noise) and its hierarchical version HDBSCAN, and what the indices cannot tell you

Structure is free, labels are expensive

Clustering receives only the table \(X\), no labels, and returns groups of similar rows.

segment

customers

compress

a palette of \(k\) colors is a clustering of the pixels

organize

documents: the road to topic modeling

The price: there is no answer key. The algorithm always returns a partition, whether or not the data has groups.

The path of this lesson

  1. Define and judgedistance, internal and external indices
  2. k-meansLloyd's algorithm by hand, local minima
  3. Choosing kelbow, silhouette, other indices, scaling
  4. Beyond centerswhere k-means fails; DBSCAN, HDBSCAN
  5. Choose and interpretwhich algorithm, what each group is
Each stage answers one question: what makes a partition good when there is no answer key? → how do we find groups around centers? → how many groups, and can the indices tell? → what if the groups are not round blobs? → which algorithm, and what does each group mean?
Stage 1 · What makes a partition good when there is no answer key?

What clustering is, and how it is judged

classificationclustering
input\(X\) and \(y\)only \(X\)
"correct" meanspredicting \(y\) on new rowssatisfying a criterion you chose
validated witha held-out test setno test set is possible without labels

what "similar" means

a distance adds up columns: grams drown millimeters. Scaling is part of the clustering.

how it is judged

internal indices use only \(X\); external ones (the Adjusted Rand Index, ARI) compare with known labels, here only to check the internal ones.

The Adjusted Rand Index, by hand

Truth: \(\{1,2,3\}, \{4,5,6\}\). A clustering: \(\{1,2\}, \{3,4\}, \{5,6\}\).

  • Of the 15 pairs, they agree on 10: (1,2) and (5,6) together in both; 8 cross pairs apart in both.
  • Rand index \(= 10/15 = 0.667\). But random labels also agree on many pairs.
  • The ARI subtracts chance: 0.242.
Predict. A perfect partition with the group names swapped (\(0 \leftrightarrow 1\)): what is its ARI? And two unrelated random partitions of 1,000 points?

Answers: 1.0 (the ARI ignores names) and 0.0.

Stage 2 · How do we find groups around centers?

k-means: points near a center

\[ J = \sum_{j=1}^{k} \sum_{x_i \in C_j} \lVert x_i - \mu_j \rVert^2, \qquad \mu_j = \text{mean of } C_j \]

The inertia: each point's squared distance to the center of its group. Circular: centers depend on groups, groups on centers. Lloyd's algorithm alternates:

  1. assign: each point goes to its nearest center;
  2. update: each center moves to the mean of its points.
Neither step can increase \(J\), so it always stops. Not necessarily at the best partition.

Lloyd by hand: a bad start

Two unit squares, around \((1.5, 1.5)\) and \((6.5, 5.5)\). Both centers start in the left one: \(c_1 = (1,1)\), \(c_2 = (2,2)\).

pointcoordinates\(d^2\) to \(c_1\)\(d^2\) to \(c_2\)group
\(x_1, x_2, x_3\)(1,1), (2,1), (1,2)0, 1, 12, 1, 11 (ties go to the first)
\(x_4\)(2, 2)202
\(x_5 \dots x_8\)the right square41 to 6125 to 412
Predict. \(x_4\) joined the group of the four points six units away. Will the algorithm notice its mistake?

Three iterations

Iteration 0: x4 joins the right group, inertia 134; iteration 1: the centers separate and x4 returns, inertia 9.42; iteration 2: nothing changes, inertia 4.00
  • Update: \(c_2\) = mean of \(x_4 \dots x_8\) = \((5.6, 4.8)\), dragged to the right; \(x_4\) is now closer to \(c_1\) (0.89 against 20.80) and switches back.
  • Inertia 134 → 9.42 → 4.00: the first iteration does almost all the work. Convergence = no assignment changed.

Simulator: Lloyd, one step at a time

The spokes join each point to its center; the inertia is the sum of their squared lengths. Its history never goes up.

Simulator: try to beat the mean

Try. Drag the centers and watch which group contributes most to the inertia. Then move every center to its mean.

Initialization and local minima

300 points in three well-separated blobs; k-means with one start, 100 seeds.

Predict. With random starting centers, in how many of the 100 runs does k-means get stuck?
Global minimum 566.9; a local minimum where two centers share one blob; random init stuck in 22 of 100 runs, k-means++ in 0

Random starts: stuck in 22 of 100 runs (inertia up to 5,703.3, ten times the best, 566.9). k-means++: 0. Random with n_init=10 (restart, keep the lowest inertia): 0. Two centers share one blob, and moving any single center or point makes it worse.

Stage 3 · How many groups, and can the indices tell?

Choosing \(k\): the inertia cannot

  • Adding a center can only lower the inertia; with \(k = n\) it is 0.
  • The elbow: the \(k\) after which a new group buys much less. A heuristic, often ambiguous, read on a curve that always goes down.
Choosing \(k\) needs criteria that do not automatically improve with more groups.

The silhouette, by hand

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

\(a\): mean distance to its own group; \(b\): to the nearest other group. Near 1: comfortable; near 0: on a border; negative: closer to another group.

\(x_1 = (1,1)\), the converged squares

\(a = (1 + 1 + 1.414)/3 = 1.1381\)

\(b\) = mean of 6.403, 7.211, 7.071, 7.810 = 7.1239

\(s = (7.1239 - 1.1381)/7.1239 = \mathbf{0.8402}\)

all eight points

outer corners 0.8402, inner corners 0.8011

0.8223
the mean: two small squares five units apart

Simulator: the silhouette of every point

Click a point to see its two circles: \(a\) in its own color, \(b\) in the neighbor's. The bars are every point's silhouette, sorted by group.

Two more internal indices

Calinski–Harabasz, CH (higher is better)

\[ \mathrm{CH} = \frac{B/(k-1)}{W/(n-k)} \]

Eight points: \(B = 82\), \(W = 4\): \(\mathrm{CH} = (82/1)/(4/6) = 123\)

Davies–Bouldin, DB (lower is better)

for each group, its worst neighbor: \((s_i + s_j)/d(\mu_i, \mu_j)\)

Eight points: \((0.7071 + 0.7071)/6.4031 = 0.2209\)

Same ingredients as the silhouette: compactness against separation.

On real data: 342 penguins

Four standardized measurements; the species (Adelie 151, Gentoo 123, Chinstrap 68) kept aside, only for the ARI.

\(k\)inertiasilhouetteCHDBARI (check)
2565.70.532482.20.7140.655
3379.40.447441.70.9440.793
4300.40.400400.40.9440.637
5232.60.378411.20.9730.572
Predict. Cover the last column. Which \(k\) would you choose? Then uncover it.

Two different questions

Inertia against k with an elbow at k=3; silhouette maximal at k=2 (0.532) while the ARI with the species is maximal at k=3 (0.793)
  • The elbow says 3 (the inertia drops 186 from 2 to 3, then only 79); silhouette, CH and DB all say 2; the species are 3.
  • Adelie and Chinstrap differ mainly in bill length: merging them really is more compact.
"How separated is this partition" and "is this the partition I want" are different questions. No internal index answers the second.

Simulator: sweep \(k\) on eight datasets

Try. Clean blobs, unequal sizes, circles, noise: when do the elbow and the silhouette agree?

The silhouette of pure noise

300 uniform points: no structure at all.

\(k\)2345
silhouette0.3590.3790.40.382
A silhouette of 0.40 out of pure noise, in the same range as the penguins. It is not a certificate that groups exist.

A ruler: rerun the same pipeline with each column shuffled independently and report both silhouettes. The lab does it.

Scaling matters, and the silhouette will not tell you

Standard deviations: body mass 801.95 g, bill depth 1.97 mm, about 400 times smaller.

Predict. \(k = 3\) on raw units or on standardized columns: which has the higher silhouette? Which finds the species?
silhouetteARI with species
raw units0.5800.331
standardized0.4470.793

Raw: three weight bands, well separated. An internal index compares partitions of the same space; when the scaling changes, the ruler changes. Scale by an argument about units.

Stage 4 · What if the groups are not round blobs?

What k-means assumes

Each group is a convex cell with straight boundaries halfway between centers, and every point pays the same squared distance:

  • groups are convex and roughly round;
  • groups have similar spread;
  • there is no noise: every point is assigned somewhere;
  • you know \(k\).

Five shapes, k-means told the right \(k\)

data\(k\)silhouetteARI
blobs30.8231.000
moons20.4890.253
circles20.356−0.002
anisotropic30.5010.555
densities30.6050.436
Only the blobs are recovered. "Densities", all three groups wrong, has the second-highest silhouette: the metric shares the algorithm's idea of a good group.

Simulator: where k-means fails

Try. Cycle through the shapes and vary \(k\). Is there a \(k\) that fixes a ring?

Hierarchical clustering

k-means returns one flat partition for one chosen \(k\). Agglomerative clustering builds the whole tree of merges, the dendrogram, and lets you choose the number of groups afterwards by cutting it.

It has its own lesson, where the linkages (single, complete, average, Ward) are worked by hand.

DBSCAN: dense regions

A group is a dense region, separated by sparse ones. Two parameters, eps and min_samples:

core

at least min_samples points within eps, itself included

border

not core, but within eps of a core point

noise

neither: label −1, not forced into a group

A cluster grows only through core points. Any shape, and nobody chooses the number of clusters.

DBSCAN by hand: ten points, eps = 1.5, min_samples = 4

Eight core points in two squares; E is a border point reached from core D; F is noise because its only neighbor E is not core

A, B, C, D: the left square (D also sees E); G, H, I, J: the right one. E = (3, 3) sees D and F; F = (4, 4) sees only E.

Predict. F is within eps of E, and E belongs to a cluster. Is F in the cluster?

No: E has only 3 points within 1.5, so it is a border point, and clusters grow only through core points. F is noise.

Choosing eps: one value, one density

The k-distance plot: each point's distance to its \(k\)-th neighbor, sorted; read eps at the knee. "Densities": the tight groups need about 0.07, the loose one about 0.3.

The sorted distance to the 5th neighbor has no single knee; eps 0.15 shatters the loose group, eps 0.4 merges the tight ones, HDBSCAN finds the 3 groups
eps0.150.30.60.8
clusters8621
noise1726354
ARI0.8250.3970.619−0.003
Nowhere in the sweep do the three groups come out. One global eps means one density.

HDBSCAN: every eps at once

Runs DBSCAN for every eps, builds a hierarchy of clusters and keeps those that persist the longest, each at its own density. One parameter: min_cluster_size.

Top: k-means with the true k, ARI 1.00, 0.25, 0.00, 0.56, 0.44. Bottom: HDBSCAN with no k, ARI 1.00, 1.00, 1.00, 0.85, 0.86
HDBSCAN, no \(k\)blobsmoonscirclesanisotropicdensities
clusters · noise3 · 02 · 02 · 03 · 493 · 45
ARI1.0001.0001.0000.8520.856

The noise points are tails and fringes; the assigned ones are all right. This is why BERTopic uses HDBSCAN.

Stage 5 · Which algorithm, and what does each group mean?

Choosing an algorithm

k-meanshierarchical (Ward)DBSCANHDBSCAN
a group isnearest a centera branch of mergesa dense regiondense, at its own density
you choose\(k\)the cut (\(k\))eps, min_samplesmin_cluster_size
shapesconvex, rounddepends on the linkageanyany, mixed densities
noiseforced inforced in−1−1
new pointspredictnonono
Start with k-means; switch for a concrete reason: non-convex groups, different densities, outliers, or no way to justify a \(k\).

Four questions before believing a clustering

  1. Did I scale? If not, it probably sorts by the column with the largest unit.
  2. Did I restart enough? An inertia ten times too high is possible.
  3. Where did \(k\) come from? The silhouette preferred 2 penguin groups where there are 3 species.
  4. Did I compare with shuffled data? Pure noise reaches a silhouette of 0.40.

Interpreting clusters

Back to the original columns, with a groupby:

clusterbill lengthbill depthflippermass (g)n
047.515.0217.25076.0123
138.218.1188.43584.7132
247.518.8196.93902.087

Heavy birds with long flippers and shallow bills; long and deep bills; small birds with short bills. (Cluster 0 is exactly the Gentoo; the only confusion is Adelie against Chinstrap.)

Describe a choice, not a discovery: "we partitioned the data into 3 groups after standardizing, chose \(k\) by this criterion, and the result is this far from shuffled data".

Summary

  1. No labels, no answer key: the algorithm always returns a partition.
  2. k-means minimizes the inertia by alternating assign and update; restart it (n_init).
  3. Silhouette, CH, DB measure separation, not whether the partition is the one you want; noise scores 0.40.
  4. Scale by an argument about units, not by an index.
  5. k-means wants round groups; DBSCAN and HDBSCAN find dense regions and mark noise.
Lab

Clustering 178 wines

  • 13 chemical measurements; the cultivar column is sealed until the last step: every decision is taken without labels.
  • Download the lab notebook and open it in Jupyter or Colab.

1. load, and seal the cultivar
2. scale, and why the indices cannot decide it
3. choose \(k\): elbow and silhouette

4. the ruler: shuffled data
5. fit \(k = 3\), inspect each cluster
6. profile in the original units

7. try DBSCAN and HDBSCAN
8. look at the clusters with PCA (principal component analysis)
9. open the seal: audit every decision