Clustering

core 12 minunsupervisedclustering

Builds on:Dot product, norms, and similarityCommon distributionsFeature scaling and preprocessing

The idea

Clustering asks: do these unlabeled points form groups? k-means makes that concrete by choosing kk centroids μj\mu_j to minimize within-cluster squared distance (inertia):

J=∑j=1k∑xi∈Cj∥xi−μj∥2J = \sum_{j=1}^{k} \sum_{x_i \in C_j} \|x_i - \mu_j\|^2

Lloyd's algorithm solves it by alternating two moves, each of which can only lower JJ:

  1. Assign each point to its nearest centroid.
  2. Update each centroid to the mean of its points (the mean is the unique minimizer of squared distances — that is why it is k-means).

Finitely many partitions + monotone descent ⇒ guaranteed convergence, but to a local minimum: initialization decides which one. Standard practice is k-means++ seeding (new seeds picked with probability proportional to squared distance from existing seeds, spreading them out) plus a few restarts, keeping the lowest-inertia solution. Cost per iteration is O(nkd)O(nkd) — this is the clustering algorithm that scales.

The objective silently assumes: Euclidean geometry (so scale your features), convex roundish clusters of similar size and density, and a known kk. Each broken assumption has a designated successor:

Choosing k (for the methods that need it): the inertia-vs-kk curve only ever decreases, so look for the elbow where the marginal drop collapses; cross-check with the silhouette

si=bi−aimax⁡(ai,bi)s_i = \frac{b_i - a_i}{\max(a_i, b_i)}

(aia_i = mean distance to own cluster, bib_i = mean distance to nearest other cluster; average sis_i near 1 is tight-and-separated, near 0 is mush) — and then let the domain veto: a segmentation nobody can act on has the wrong kk regardless of the curves.

Worked example

Six 1-D points, k=2k = 2: {1,2,3,10,11,12}\{1, 2, 3, 10, 11, 12\}, initial centroids μ1=1\mu_1 = 1, μ2=12\mu_2 = 12.

Iteration 1 — assign: distances to (μ1,μ2)(\mu_1, \mu_2): point 3 is ∣3−1∣=2|3-1| = 2 vs ∣3−12∣=9|3-12| = 9 → cluster 1; point 10 is 99 vs 22 → cluster 2. Result: C1={1,2,3}C_1 = \{1,2,3\}, C2={10,11,12}C_2 = \{10,11,12\}.

Iteration 1 — update: μ1=(1+2+3)/3=2\mu_1 = (1+2+3)/3 = 2, μ2=(10+11+12)/3=11\mu_2 = (10+11+12)/3 = 11.

Iteration 2 — assign: no point changes side → converged.

J=(1+0+1)+(1+0+1)=4J = (1{+}0{+}1) + (1{+}0{+}1) = 4

Silhouette of point 3 (the boundary-most point of cluster 1):

a3=∣3−1∣+∣3−2∣2=1.5,b3=7+8+93=8,s3=8−1.58≈0.81a_3 = \frac{|3-1| + |3-2|}{2} = 1.5, \qquad b_3 = \frac{7 + 8 + 9}{3} = 8, \qquad s_3 = \frac{8 - 1.5}{8} \approx 0.81

Even the worst-placed point scores 0.81 — this is what genuinely separated clusters look like. Contrast k=3k = 3 on the same data: the best it can do is split one tight triple, say into {1}\{1\} and {2,3}\{2, 3\}. Inertia falls to 0+0.5+2=2.50 + 0.5 + 2 = 2.5 — lower than 4, as inertia always is with more clusters — but the silhouette collapses where the split happened: point 2 now has a2=∣2−3∣=1a_2 = |2-3| = 1 and b2=∣2−1∣=1b_2 = |2-1| = 1 (distance to the singleton cluster), so s2=0s_2 = 0, and the singleton {1}\{1\} gets s1=0s_1 = 0 by convention. Inertia rewards k=3k = 3; silhouette exposes it. That disagreement is the everyday argument for never choosing kk from the elbow curve alone.

Cheat sheet

k-means objective: J=∑j∑i∈Cj∥xi−μj∥2J = \sum_j \sum_{i \in C_j} \|x_i - \mu_j\|^2; Lloyd: assign-to-nearest / recompute-means; converges to local min; O(nkd)O(nkd) per iter; k-means++ + restarts.

Silhouette: si=(bi−ai)/max⁡(ai,bi)∈[−1,1]s_i = (b_i - a_i)/\max(a_i, b_i) \in [-1, 1]; average it to score a clustering; near 0 ⇒ overlapping/boundary.

MethodAssignmentsShape assumptionNeeds k?Scales?Reach for when
k-meanshardspherical, similar sizeyesgreatlarge n, fast segmentation
GMM (EM)soft probabilitieselliptical (per-cluster covariance)yesokayoverlap, membership probabilities, density model
Hierarchical (Ward etc.)nested dendrogramlinkage-dependentno — cut laterO(n2)O(n^2), small ntaxonomy, unknown k, custom distances
DBSCANhard + noise labelarbitrary shape, uniform densityno (ε\varepsilon, minPts instead)goodnon-convex shapes, outlier discovery