Clustering
k-means, DBSCAN with its epsilon-neighbourhood, and hierarchical clustering.
Unsupervised learning, part two. Three families of algorithm, one defining question each.
Clustering versus classification
> Classification uses prior knowledge of classes. Clustering discovers groups without them.

The slide makes the contrast concrete on the diabetes data: in classification you know which patients are diabetic and learn to predict it; in clustering you get "Cluster 1" and "Cluster 2" and only afterwards notice that they happen to correspond to diabetic and non-diabetic patients.
The three families
| Family | Basis of grouping | Algorithms |
|---|---|---|
| Centroid-based (partition) | closeness of points to a central value | k-means |
| Density-based | regions of high concentration of points | DBSCAN |
| Connectivity-based (hierarchical) | each point connected to neighbours by proximity | agglomerative / dendrogram |

k-means
Goal: predict $K$ centroids and a label for each data point — group $N$ examples into $K$ clusters.
Assumptions (all three matter, because each is a failure mode):
- samples $x_1,\dots,x_N$ lie in Euclidean space
- each observation belongs to one of the $K$ clusters (hard assignment)
- clusters are non-overlapping

k-means algorithm
- Initialise $K$ cluster centres randomly.
- Assign each sample to the closest centroid (by distance).
- Update each centroid to the mean of the points now assigned to it.
- Repeat 2–3 until the assignments stop changing (or a maximum iteration count).

It minimises the within-cluster sum of squares
$$J = \sum_{k=1}^{K}\sum_{x \in C_k}\|x - \mu_k\|^2$$
which is guaranteed to decrease at every step, so k-means always converges — but only to a local optimum, which is why initialisation matters (k-means++ exists for that reason).
Failure modes, both shown on the deck:
- Non-spherical clusters. k-means carves the space into convex Voronoi cells, so two interleaving crescents get cut in half.
- $K$ must be chosen. Unlike DBSCAN and hierarchical clustering, k-means cannot infer the number of clusters.

Application shown: image segmentation, where each pixel's RGB vector is clustered and the cluster index becomes the colour. It is the same KMeans call on a reshaped image array.
DBSCAN
Density-Based Spatial Clustering of Applications with Noise. Identifies clusters as regions of high density separated by regions of low density, infers the number of clusters from the data, and can find arbitrary shapes.

The epsilon-neighbourhood — the definition that gets examined
> For some real value $\epsilon>0$ and some point $p$, the $\epsilon$-neighbourhood of $p$ is the set of data points at most $\epsilon$ distance away from $p$: > $$N_\epsilon(p) = \{\,q \in D \;:\; \operatorname{dist}(p,q) \le \epsilon\,\}$$

The two parameters
- $\epsilon$ — radius of the neighbourhood around a data point
- minPts — minimum number of samples that must lie in a neighbourhood for it to define a cluster
The three labels
- Core point — at least minPts points fall within its $\epsilon$-neighbourhood (including itself).
- Border point — fewer than minPts neighbours within $\epsilon$, but it lies within $\epsilon$ of a core point.
- Noise point (outlier) — neither a core point nor a border point → labelled $-1$.

The algorithm
> 1. Form a separate cluster for each core point, or each connected group of core points (reachability chains the core points together). > 2. Assign each border point to the cluster of its corresponding core point. > 3. Everything left is noise.

Sensitivity: the deck shows the same moons data at minPts = 5 and minPts = 50. Too small and noise becomes clusters; too large and everything collapses into one cluster or into noise. $\epsilon$ has the same knife-edge behaviour.
k-means vs DBSCAN
| k-means | DBSCAN | |
|---|---|---|
| Number of clusters | must be given | inferred |
| Cluster shape | convex / spherical | arbitrary |
| Outliers | forced into a cluster | explicitly labelled noise |
| Parameters | $K$ | $\epsilon$, minPts |
| Cost | fast, $O(NKd)$ per iteration | needs a spatial index; degrades in high $d$ |
| Fails when | clusters are non-convex | density varies a lot across the dataset |

Hierarchical clustering
> Clusters based on distances between observations, represented as a dendrogram rather than a flat partition. No need to specify $K$ upfront — you cut the tree afterwards.

Bottom-up (agglomerative) procedure: each sample starts as its own cluster; the closest pair of clusters is merged iteratively. Samples that fuse later are more different than samples fused earlier — the height of the merge is the dissimilarity.

Linkage — the only real choice
| Linkage | Distance between two clusters is defined as | Character |
|---|---|---|
| Single | the distance between the closest (most similar) pair of members | can chain through a bridge of points; long straggly clusters |
| Complete | the distance between the most dissimilar pair | compact, equal-diameter clusters; sensitive to outliers |
| Average | the mean pairwise distance | compromise |
| Centroid | distance between cluster centroids | can produce inversions in the dendrogram |

The complete-linkage algorithm in five steps: each point is a cluster → compute all pairwise cluster distances → merge the closest two → update the distance matrix → repeat until one cluster remains.

Different linkage gives a different dendrogram from the same data. If an exam question shows two dendrograms and asks why they differ, the answer is the linkage rule (or the distance metric), not the data.
"How does k-means work?" — full-mark structure. State the objective ($\min \sum_k\sum_{x\in C_k}\|x-\mu_k\|^2$), then the four steps (initialise $K$ centroids randomly → assign each point to the nearest centroid → recompute centroids as the means → repeat until assignment is stable), then the two properties that show understanding: it always converges because the objective decreases monotonically at each step, but only to a local optimum, so initialisation matters; and it assumes spherical, non-overlapping, equally-sized clusters, so it fails on non-convex shapes and requires $K$ to be supplied. Contrast with DBSCAN in one line (infers $K$, arbitrary shapes, explicit noise) to close.


































