← All lessons
0197

Clustering

Everything so far learned from labels or rewards. Unsupervised learning has neither — just raw data and the question: is there structure here? Clustering is its classic move, and k-means the classic algorithm: pick k centres, then alternate two steps until the groups stop changing.

Press the button repeatedly and watch the loop: ASSIGN each point to its nearest centre, then MOVE each centre to the mean of its points. Within a few rounds the centres migrate into the three natural blobs — groups nobody labelled. Then change k: the algorithm happily carves the data into however many clusters you demand, right or wrong. Choosing k, and judging whether the clusters MEAN anything, remains human work. Real uses: customer segments, gene expression groups, organising unlabelled documents.

iterations: 0

Grey points, no labels anywhere — the algorithm must discover structure on its own. Squares are the k centres, dropped in arbitrarily.

Variants & real-world flavors

K-means is the fastest and simplest but assumes round, similarly sized clusters and needs k chosen up front. Hierarchical clustering builds a tree of merges instead — cut it at any height to get any number of clusters, and the dendrogram itself is informative. DBSCAN defines clusters by density: it finds arbitrarily shaped clusters and, crucially, is allowed to answer this point belongs to no cluster — making it a favourite for noisy data (HDBSCAN is its modern refinement). Gaussian mixture models are k-means gone probabilistic: each point gets soft membership percentages rather than one hard label, and clusters can be elliptical.

Check yourself

K-means always produces exactly k clusters, even on pure random noise. Why is that a trap?

Go deeper (free): Naftali Harris — Visualizing k-means (interactive)

Next: PCA & Dimensionality