CAB107 · Unit 5: Artificial intelligence techniques
K-Means, hierarchical clustering, DBSCAN and PCA
30 practice questions · in the ETE syllabus
Key points
- K-Means needs K and finds compact clusters
- DBSCAN finds arbitrary shapes and marks noise
- Core, border and noise points depend on eps and minPts
- PCA keeps maximum variance and needs scaled features
- Density-reachability chains run through core points
- Choose eps from the knee of a k-distance plot
- Linkage sets the distance between clusters in hierarchical clustering
- Elbow and silhouette help choose K
Clustering groups unlabelled points by similarity. K-Means needs the number of clusters K in advance; it assigns each point to the nearest centroid and recomputes centroids until they stabilise, tending to give compact, roughly spherical clusters. Hierarchical clustering builds a tree (dendrogram) of merges, and cutting it at a height gives the clusters.
DBSCAN groups points by density using eps (neighbourhood radius) and minPts. A core point has at least minPts points within eps, counting itself; a border point is within eps of a core point but is not core; the rest are noise. It finds clusters of arbitrary shape and needs no preset cluster count.
Worked example: points 1, 2, 3, 10 with eps = 1.5 and minPts = 3. Only point 2 has three points within 1.5, so it is the single core point; points 1 and 3 are border points and 10 is noise.
PCA (principal component analysis) reduces the number of dimensions while keeping as much variance as possible, by projecting onto orthogonal directions of maximum variance. It is unsupervised.
from sklearn.decomposition import PCA
Z = PCA(n_components=2).fit_transform(X_scaled)
K-Means and PCA depend on distances or variance, so features with large ranges would dominate unless scaled.
Exam trap: the K in K-Means is the number of clusters; the k in k-NN is the number of neighbours.
DBSCAN in more detail
DBSCAN (density-based spatial clustering of applications with noise) has two parameters: eps, the radius of a neighbourhood, and minPts, the number of points (counting the point itself, as scikit-learn's min_samples does) a neighbourhood needs to be dense.
- q is directly density-reachable from p if p is a core point and q lies within eps of p.
- q is density-reachable from p if there is a chain p, p1, ..., q in which each step is directly density-reachable, so every point on the chain except possibly the last is a core point.
- p and q are density-connected if both are density-reachable from some core point o. A cluster is a maximal set of density-connected points.
Algorithm: pick an unvisited point; if it is core, start a cluster and keep adding every point density-reachable from it; if it is not core and no core point reaches it, it is noise for now. Core points and noise are fixed by the data, but a border point within eps of cores from two clusters goes to whichever cluster reaches it first.
Choosing the parameters. A common rule is minPts of at least the number of dimensions plus one (larger for noisy data). For eps, draw a k-distance plot: sort every point's distance to its k-th nearest neighbour (k tied to minPts) and choose eps at the knee of the curve. Too small an eps turns most points into noise; too large an eps merges separate clusters.
| K-Means | DBSCAN | |
|---|---|---|
| Needs | K | eps, minPts |
| Cluster shape | Compact, roughly spherical | Any shape |
| Outliers | Forced into a cluster | Labelled noise (-1 in scikit-learn) |
| Weak when | Shapes are non-convex | Densities vary a lot, or in high dimensions where distances become similar |
from sklearn.cluster import DBSCAN
labels = DBSCAN(eps=0.5, min_samples=5).fit_predict(X_scaled) # -1 = noise
Hierarchical clustering and choosing K
Agglomerative clustering starts with every point as its own cluster and repeatedly merges the closest pair; divisive clustering starts with one cluster and splits it. The distance between clusters is set by the linkage: single (closest pair of points, can produce long chains), complete (farthest pair), average (mean of all pairs) and Ward (the merge that least increases total within-cluster variance). Cutting the dendrogram at a height h keeps only merges made at distances up to h. Storing all pairwise distances needs O(n^2) memory, so it suits small data sets.
For K-Means, the elbow method plots the within-cluster sum of squares (inertia) against K and picks the K after which the drop flattens. The silhouette of a point is (b - a) / max(a, b), where a is its mean distance to its own cluster and b to the nearest other cluster; it runs from -1 to 1, and a negative value suggests the point sits in the wrong cluster.
Try two questions
Which pair of clusters does Ward's linkage merge at each step?
- The pair adding the least within-cluster variance
- The two clusters holding the most points
- The pair whose closest points are farthest apart
- A random pair chosen among neighbouring clusters
Show the answer
A. Ward's method merges the two clusters whose union causes the smallest increase in the total within-cluster sum of squares, so it tends to give compact clusters like K-Means.
Why not B: Cluster size alone does not decide a merge.
Why not C: Merging the farthest pair would do the opposite of clustering.
Why not D: Hierarchical clustering is deterministic, not random.
One-dimensional points 1, 2, 3, 4, 8, 9, 10 and 20 are clustered by DBSCAN with eps = 1.5 and minPts = 3 (the point itself counts). What is the result?
- One cluster, with no noise
- Three clusters, with no noise
- Two clusters, with 20 as noise
- Two clusters, with 4, 8 and 20 as noise
Show the answer
C. Core points are 2, 3 and 9 (each has three points within 1.5). Points 1 and 4 are border points of cluster {1, 2, 3, 4}; 8 and 10 are border points of {8, 9, 10}. Point 20 has no neighbour, so it is noise.
Why not A: The gap from 4 to 8 is larger than eps, so the groups cannot join.
Why not B: Point 20 cannot form a cluster on its own.
Why not D: Points 4 and 8 are border points reached by cores 3 and 9.