Lecture 6 · Animations
Dimensionality reduction and unsupervised learning
Two of today's clustering methods, in two dimensions where you can see everything. Press Step to advance one unit of the algorithm and read what happened; press Play to let it run. Every number in the side panel is recomputed at every step.
All data on this page is synthetic, generated in your browser from a fixed seed, so Reset always gives back the same picture.
1k-means, one half-step at a time
Lloyd's algorithm alternates two moves: assign every point to its nearest centroid, then update every centroid to the mean of its points. Each press of Step is one of the two. The shading is the Voronoi diagram of the current centroids. On the right, the best inertia and silhouette found for each $k$ from ten k-means++ starts; the ring is the run in front of you.
The inertia of an assignment $c$ and centroids $\boldsymbol\mu$:
$$J = \sum_{i=1}^{m}\bigl\lVert\mathbf{x}^{(i)} - \boldsymbol\mu_{c(i)}\bigr\rVert_2^2.$$Assign minimises $J$ over $c$ with $\boldsymbol\mu$ fixed — each term is smallest at the nearest centroid. Update minimises it over $\boldsymbol\mu$ with $c$ fixed, because for any points $\sum_i\lVert\mathbf{x}^{(i)}-\boldsymbol\mu\rVert^2 = \sum_i\lVert\mathbf{x}^{(i)}-\bar{\mathbf{x}}\rVert^2 + n\lVert\bar{\mathbf{x}}-\boldsymbol\mu\rVert^2$, smallest at the mean. So neither half-step can raise $J$. The silhouette of point $i$ is $s_i = (b-a)/\max(a,b)$, with $a$ its mean distance to its own cluster and $b$ to the nearest other one.
k-means++ picks the first centroid uniformly, then each next one with probability proportional to its squared distance $D(\mathbf{x})^2$ from the nearest centroid already chosen.
- The inertia curve under the plot only ever goes down, on both kinds of half-step, and stops when an assign step changes nothing.
- On the right, the best $J$ keeps falling as $k$ grows, past the four groups that are really there. The silhouette peaks at $k = 4$.
- Converged is not the same as best: a start can stop with a ring well above the curve, two centroids sharing one group.
- Choose random initialisation and press New start once (start 2), then Play: two centroids end up splitting the large group, and the final inertia is about twice the best.
- Press New start ten times under each initialisation and
count the rings that stop well above the curve. Random starts end
badly more often — over 200 starts on this data, 62% against 34% for
k-means++ — and k-means++ still does sometimes, which is why
n_initexists. - Set $k = 5$ and read the curves on the right: the best inertia is lower than at $k = 4$, and the silhouette is lower too.
2DBSCAN on shapes k-means cannot cut
Two interleaved half-moons and a scatter of background points.
Large markers are core points: at least
min_samples points, counting themselves, within $\varepsilon$.
Each Step expands one core point — the dashed circle is its
$\varepsilon$-neighbourhood — and every unlabelled point inside joins its
cluster. What no cluster reaches is noise (×).
With $N_\varepsilon(\mathbf{x}) = \{\mathbf{x}' : \lVert\mathbf{x}'-\mathbf{x}\rVert \le \varepsilon\}$, the deck's three definitions:
- $\mathbf{x}$ is core if $|N_\varepsilon(\mathbf{x})| \ge$
min_samples; - core points within $\varepsilon$ of each other share a cluster, and so do the points in their neighbourhoods;
- everything else is noise, label $-1$.
A cluster is therefore the set reachable from one core point by hops of length at most $\varepsilon$ through core points — a breadth-first search, which is what Step performs. The score is the purity against the generating groups (moon, moon, background): the share of points whose cluster is mostly made of their own group, with noise counted as one more cluster.
- A cluster grows along its moon, around the bend, because it only ever looks $\varepsilon$ ahead. Its shape is not constrained at all.
- Hollow markers are border points: in a cluster, but not core, so the search does not continue through them.
- Tick k-means, k = 2: its boundary is a straight line, because two Voronoi cells are two half-planes, and it cuts both moons.
- Set $\varepsilon = 0.12$ and press Play: the moons break into about ten pieces and many points become noise.
- Set $\varepsilon = 0.40$: the gap between the moons is now shorter than one hop, and a single cluster swallows both of them.
- At $\varepsilon = 0.25$, raise
min_samplesto 10: fewer core points, more hollow border points and more noise, and the moons now break into four clusters. Density is $\varepsilon$ andmin_samplestogether.