Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Machine Learning Foundations — Alpha School

Lesson 13: Clustering and Dimensionality Reduction

So far every model in this course has learned from labelled answers. Now the labels disappear. In unsupervised learning the model must find structure in the data by itself, and the most useful kind of structure is a group. A shop wants to know what kinds of customers it has. A biologist wants to know whether a collection of cells falls into natural types. A streaming service wants to group films by what they have in common. None of these questions comes with right answers attached, so the model groups examples by similarity instead. That is clustering.

The best-known clustering method is k-means. You choose how many groups, k, you want, and the algorithm places k centres among the data, assigns each example to its nearest centre, then moves each centre to the middle of its group, and repeats until the groups stop changing. The result is k groups of similar examples. The hard part is choosing k: too few, and genuinely different groups are lumped together; too many, and one real group is split into meaningless pieces. A common trick is to try several values of k and look for the point where adding another group stops improving the fit much — the elbow in a graph of fit against k.

Seeing data with too many measurements

Clustering has a cousin problem: real data often has far too many measurements to look at. A dataset of houses might have a hundred features; a dataset of gene activity might have thousands. Humans cannot picture a hundred dimensions, and models can struggle with them too, because in very high dimensions everything starts to look equally far apart. Dimensionality reduction squeezes the data down to a few numbers that still capture what matters most. The classic method is principal component analysis, or PCA, which finds new directions — combinations of the original features — along which the data varies the most, and keeps only the top few.

PCA is often used just to draw a picture: take data with hundreds of features, reduce it to two principal components, and plot the result to see whether groups or patterns appear. It is also used before clustering, because clustering works better when the noise has been squeezed out. But it has a cost: the new directions are mixtures of the original features, so they are hard to explain. The first principal component of house data might be a mix of size, price and location, and no one can say in plain words what it means. That is the recurring trade of this unit: unsupervised methods find real structure, but the structure they find is not always easy to name.

Why scaling matters for clustering

Clustering has a trap that supervised models often avoid: it measures distance, and distance depends on scale. Imagine clustering customers by age and by yearly spending. Age ranges over perhaps eighty years, while spending ranges over tens of thousands — so without any adjustment, spending will dominate the distance completely, and age will barely matter. Two customers of the same age but different spending will look far apart, while two customers of very different ages but similar spending will look close. The fix is to scale the features first, so each one contributes fairly — for example, by adjusting each feature to have a similar range. The same applies to PCA, which also works with distances and variation: a feature measured in thousands will swamp one measured in tens, whatever their real importance. Scaling is a small step, but forgetting it is one of the most common reasons a clustering produces groups that look meaningless when you inspect them.

Because there are no labels, judging unsupervised learning is harder than judging the supervised models you already know. There is no accuracy to compute. Instead you ask whether the groups are useful: do the customer groups behave differently, do the gene groups match what biologists expect, does the two-dimensional picture show clear separation? If the answer is yes, the clustering has earned its keep. If the groups are arbitrary, no score can rescue them. Unsupervised learning is a tool for exploring and for asking better questions — and the questions it raises are often the most valuable thing it produces.

Three clusters, found without any labels

Group 1 Group 2 Group 3 K-means groups similar points together

No labels were given. The algorithm found the three groups from the positions of the points alone — each dashed circle is one group's centre and spread.

Key Terms

Clustering
An unsupervised task that groups similar examples together without any labels.
K-means
A clustering method that places k centres among the data, assigns each example to its nearest centre, and repeats until the groups settle.
Centre (centroid)
The middle point of a cluster, which k-means moves as the group changes.
Dimensionality reduction
Squeezing data with many features down to a few numbers that still capture what matters most.
Principal component analysis (PCA)
A dimensionality reduction method that finds the directions along which the data varies most and keeps only the top few.
Principal component
One of the new directions PCA finds: a combination of the original features, ranked by how much variation it captures.
Elbow method
Choosing the number of clusters by looking for the point where adding another group stops improving the fit much.

Discussion Questions

  1. A shop clusters its customers and finds four groups. How would you decide whether the groups are real and useful, rather than arbitrary?
  2. Why is choosing the number of clusters, k, a judgement call rather than something the data decides for you?
  3. PCA makes data drawable but harder to explain. When is that trade worth making?

Practice Questions

  1. What is clustering, and how is it different from classification?
  2. Describe k-means in two sentences.
  3. What goes wrong if you choose too many clusters?
  4. What does PCA do, in plain words?
  5. Why can't you judge clustering with accuracy?
Show answers
  1. Clustering groups similar examples without any labels; classification predicts labels learned from labelled examples. Clustering discovers structure, while classification applies structure learned from answers.
  2. It places k centres, assigns each example to its nearest centre, then moves each centre to its group's middle and repeats until the groups settle. The result is k groups of similar examples.
  3. One real group gets split into meaningless pieces. Too few clusters lumps genuinely different groups together; too many splits real groups apart.
  4. It finds the directions along which the data varies most and keeps only the top few. That squeezes many features down to a few numbers that still capture most of what matters.
  5. Because there are no labels and no right answers to compare with. Instead you ask whether the groups are useful — for example, whether the customer groups behave differently.
Keep reading to complete this lesson