AI Course · Week 9

Cluster Analysis

Finding groups nobody labelled. With no answers to learn from, clustering discovers the natural structure hidden in raw data, using k-means and more.

By Arj Machine Learning Est. reading time: 16 minutes

Until now, every problem came with a clear goal or a set of rules. Machine learning changes the game: we hand the computer data and ask it to find the pattern itself. Cluster analysis is the purest example. Nobody labels the data, nobody says what the groups are, yet the algorithm sorts thousands of points into natural clusters, customers who behave alike, cells of the same type, documents on the same theme. This page shows how, with k-means as the star, and lets you watch it converge live.

1

Learning without labels

Machine learning splits into families by what the data looks like. In supervised learning (next week) every example comes with the right answer, and the model learns to reproduce it. Cluster analysis is unsupervised: there are no answers, no labels, just raw data points. The task is to discover structure that was never pointed out.

Clustering groups data so that points in the same cluster are similar to each other and different from points in other clusters. The algorithm is not told what the groups mean or even how many there are; it finds groupings from the shape of the data alone.

Where you meet it
Customer segmentation (shoppers who behave alike), image compression (grouping similar colours), document topics, biology (cell types), and anomaly detection all lean on clustering. Anywhere you suspect hidden groups but cannot label them, clustering is the tool.
2

Measuring closeness

Clustering is built on one idea: similar things are close together. So we need a way to measure distance between two data points. Each point is a list of numbers (its features), and the most common measure is plain Euclidean distance, the straight-line distance you would measure with a ruler.

Point A = (x1, y1) Point B = (x2, y2) distance = sqrt( (x1 - x2)^2 + (y1 - y2)^2 )

In two dimensions this is the familiar distance on a graph; the same formula extends to any number of features. Smaller distance means more similar. Everything clustering does comes down to comparing these distances.

Scale matters
If one feature runs from 0 to 1 and another from 0 to 100000, the big one dominates the distance and the small one is ignored. In practice features are usually rescaled to a common range first, so each contributes fairly. Forgetting this is a classic beginner mistake.
3

The k-means algorithm

K-means is the most popular clustering method, and its loop is beautifully simple. You first pick k, the number of clusters you want. The algorithm then places k centroids (cluster centres) and repeats two steps until nothing changes.

  1. Assignment step: assign every point to its nearest centroid. This carves the data into k groups.
  2. Update step: move each centroid to the mean (average position) of the points assigned to it.

Then repeat: reassign every point to the nearest (now moved) centroid, move the centroids again, and so on. Each round the centroids settle a little more, until an entire round changes nothing. At that point k-means has converged and the clusters are final.

Two steps, dancing
Assign points to centres, move centres to points, repeat. Each step reduces the total distance from points to their centre, so the process always settles. It is a close cousin of the hill climbing you saw in week 5: steadily improving until it can improve no more.
4

Watch k-means converge

Reading the two steps is one thing; watching them is another. Below is a cloud of unlabelled points. Choose how many clusters to look for, then step through. Each round, points recolour to their nearest centroid, then the centroids (the large diamonds) glide to the middle of their group. Watch them lock onto the natural clusters.

Interactive K-means clustering

Dots are data points, coloured by their current cluster. Diamonds are the centroids.

Number of clusters, k:
Press step to assign points, or play to run to convergence.
Try k = 3 on this data (it was drawn from three groups) and watch it snap cleanly. Then try k = 2 or 4 to see it force the data into the wrong number of clusters.
5

Choosing k

K-means has one awkward catch: you must choose k before you start, but the whole reason you are clustering is that you do not know the structure. Pick k badly and you split one real group in two, or merge two into one. Two standard methods help.

  • The elbow method: run k-means for several values of k and plot how tightly the points hug their centroids. As k rises this always improves, but there is usually a k where the improvement suddenly slows, forming an "elbow" in the plot. That elbow is a good choice of k.
  • The silhouette score: a number for each k measuring how well points sit inside their own cluster versus how close they are to the next cluster. The k with the highest silhouette is a strong candidate.
There is rarely one right answer
These methods suggest good values of k, they do not prove one. Real data is messy, and the "right" number of clusters often depends on what you plan to do with them. Judgement is part of the job.
6

Hierarchical clustering

K-means is not the only way. Hierarchical clustering builds a whole tree of clusters instead of a single flat grouping, and it does not need you to pick k up front. The most common form works bottom-up:

  1. Start with every point as its own tiny cluster.
  2. Repeatedly merge the two closest clusters into one.
  3. Continue until everything is joined into a single cluster.

The result is drawn as a dendrogram, a tree showing which clusters merged and at what distance. To get a flat set of clusters, you simply cut the tree at a chosen height: cut low for many small clusters, cut high for a few large ones. This is its big advantage over k-means, you decide the number of clusters after seeing the structure, not before.

 K-meansHierarchical
Choose k first?YesNo, decide by cutting the tree
OutputFlat groupsA tree (dendrogram)
Speed on big dataFastSlower
7

Strengths and pitfalls

K-means is popular because it is simple and fast, but it has real limitations worth knowing for the exam and for practice.

StrengthsPitfalls
Simple, fast, scales to large dataYou must choose k in advance
Easy to understand and implementSensitive to the initial centroid placement
Works well on round, well-separated clustersStruggles with elongated or oddly shaped clusters
Gives a clear centre for each groupOutliers can drag centroids off-centre
The initialisation problem
Because k-means starts from random centroids, different runs can land on different clusterings, and a bad start can give a poor result. In practice it is run several times with different starts and the tightest result is kept, much like the random restarts you saw in local search.
8

Key points and practice

  • Clustering is unsupervised: it finds groups in unlabelled data.
  • It relies on a distance measure, usually Euclidean; rescale features first.
  • K-means repeats two steps: assign points to the nearest centroid, then move centroids to the mean, until convergence.
  • You must choose k; the elbow method and silhouette score help.
  • Hierarchical clustering builds a dendrogram and lets you pick the number of clusters afterwards.
  • K-means is fast but sensitive to initialisation and the choice of k.
3 marks
Q1. Describe the two repeating steps of the k-means algorithm.
Model answer

In the assignment step, every data point is assigned to the nearest centroid, dividing the data into k groups. In the update step, each centroid is moved to the mean position of the points assigned to it. These two steps repeat, reassigning points and moving centroids, until an entire round produces no change, at which point the algorithm has converged.

4 marks
Q2. What is the difference between supervised learning and clustering, and why is choosing k a problem?
Model answer

Supervised learning trains on data that already carries the correct answers (labels), while clustering is unsupervised and works on unlabelled data, discovering groups from the data's structure alone. Choosing k is a problem because k-means needs the number of clusters set in advance, yet the reason for clustering is that the true structure is unknown. A poor k splits genuine groups or merges distinct ones; the elbow method and silhouette score are used to estimate a sensible k.

3 marks
Q3. Give one advantage of hierarchical clustering over k-means, and one weakness of k-means.
Model answer

An advantage of hierarchical clustering is that it does not require the number of clusters to be chosen in advance; it builds a dendrogram and the number of clusters is selected afterwards by cutting the tree at a chosen height. A weakness of k-means is that it is sensitive to the initial placement of centroids, so different random starts can produce different clusterings, and it also requires k to be fixed beforehand.

Now learn with the answers
Clustering learned with no labels. Next, supervised learning uses labelled examples, starting with decision trees: models you can actually read.
Decision trees
Scroll to Top