Clustering
About this lesson
Clustering is unsupervised learning: it groups data that has no labels, by putting similar (close) points together. Watch k-means sort six café customers into two groups. It repeats two moves: every customer joins its nearest centre, then every centre moves to the mean of its customers. A poor start gets fixed, one customer switches sides and the inertia falls until nothing changes. Then see how to choose k, and why the start matters.
Groups, but no labels
Six café customers, each a dot: visits per month across, spend per visit up. Nobody labelled them. Clustering, a kind of unsupervised learning, finds groups of similar points by itself. We use the classic algorithm, k-means.
customer visits/month spend/visit (KD) 0 1 2 1 2 1 2 3 3 3 6 6 4 7 8 5 8 7 no labels: nobody says which group a customer is in clustering = unsupervised learning: find the groups
Read the lesson step by step
Groups, but no labels
Six café customers, each a dot: visits per month across, spend per visit up. Nobody labelled them. Clustering, a kind of unsupervised learning, finds groups of similar points by itself. We use the classic algorithm, k-means.
customer visits/month spend/visit (KD) 0 1 2 1 2 1 2 3 3 3 6 6 4 7 8 5 8 7 no labels: nobody says which group a customer is in clustering = unsupervised learning: find the groupsSimilar means close
k-means measures similarity as straight-line distance. From (1, 2) to (2, 1) is √2 ≈ 1.41: similar customers. From (1, 2) to (7, 8) is √72 ≈ 8.49: very different. Close points belong together.
distance = √((x1 − x2)² + (y1 − y2)²) (1, 2) → (2, 1) √(1 + 1) = √2 ≈ 1.41 (1, 2) → (7, 8) √(36 + 36) = √72 ≈ 8.49 Python: math.dist(p, q)Pick k starting centres
We ask for k = 2 groups. k-means starts with k centres, here two of the customers: c1 = (1, 2) and c2 = (3, 3). A centre (centroid) is the point that stands for its group. This start is poor, on purpose.
k = 2 (we choose k) cs = [pts[0], pts[2]] c1 = (1, 2) c2 = (3, 3) two neighbours as starting centres: a poor start, so we can watch k-means fix itAssign: join the nearest
Each customer joins its nearest centre. (2, 1) is 1.41 from c1 but 2.24 from c2, so it joins c1. (3, 3) is c2 itself, and the three on the right are nearer c2: c2's group has four.
g = assign(pts, cs) (1, 2) c1 0.00 c2 2.24 -> c1 (2, 1) c1 1.41 c2 2.24 -> c1 (3, 3) c1 2.24 c2 0.00 -> c2 (6, 6) c1 6.40 c2 4.24 -> c2 (7, 8) c1 8.49 c2 6.40 -> c2 (8, 7) c1 8.60 c2 6.40 -> c2Update: move to the mean
Each centre moves to the mean (average) of its group. c1: mean of (1, 2) and (2, 1) is (1.5, 1.5). c2: mean of its four is (6, 6). The inertia, the total squared distance to the centres, falls from 102 to 29.
new = means(pts, g) c1 = mean of (1, 2), (2, 1) = (1.5, 1.5) c2 = mean of (3, 3), (6, 6), (7, 8), (8, 7) = (24 / 4, 24 / 4) = (6, 6) inertia = sum of (distance to own centre)² 102 -> 29Assign again: one switches
Same rule, new centres. (3, 3) is now √4.5 ≈ 2.12 from c1 but √18 ≈ 4.24 from c2, so it switches to c1. Every other customer keeps its centre. The inertia drops to 15.5.
g = assign(pts, cs) (1, 2) c1 0.71 c2 6.40 -> c1 (2, 1) c1 0.71 c2 6.40 -> c1 (3, 3) c1 2.12 c2 4.24 -> c1 switched (6, 6) c1 6.36 c2 0.00 -> c2 (7, 8) c1 8.51 c2 2.24 -> c2 (8, 7) c1 8.51 c2 2.24 -> c2 inertia = 15.5Update again
c1 moves to the mean of (1, 2), (2, 1) and (3, 3): (2, 2). c2 moves to the mean of the other three: (7, 7). The inertia falls to 8.
new = means(pts, g) c1 = mean of (1, 2), (2, 1), (3, 3) = (2, 2) c2 = mean of (6, 6), (7, 8), (8, 7) = (7, 7) cs = new inertia 15.5 -> 8Nothing changes: stop
One more pass: every customer keeps its centre, so the means stay put and the loop stops. k-means has converged: occasional small spenders and frequent big spenders. No step ever raises the inertia: 102, 29, 15.5, 8.
g = assign(pts, cs) # nobody moves new = means(pts, g) # same centres if new == cs: break # True: stop centres c1 (2, 2) c2 (7, 7) groups 0 0 0 1 1 1 (0 = c1, 1 = c2) inertia 102 -> 29, 15.5 -> 8, 8 -> 8Choosing k
k-means does not choose k for you. The best inertia for k = 1, 2, 3, 4 is 83, 8, 5, 2. It keeps falling as k grows, so look for the elbow, where it stops falling fast: here, k = 2.
k best inertia 1 83 ############################ 2 8 ### 3 5 ## 4 2 # big drop from k = 1 to 2, small after: the elbow is 2 k = 6 would give 0: every customer its own groupStarts matter
k-means only finds a local best: a different start can end in worse groups. So it is often run several times from different starts, keeping the lowest inertia. The k-means++ start spreads the first centres apart.
from sklearn.cluster import KMeans km = KMeans(n_clusters=2, n_init=10).fit(pts) km.cluster_centers_ # the centres km.labels_ # the group of every point km.inertia_ # the best of the 10 runs default start: init='k-means++'
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Why is clustering called unsupervised learning?
Supervised learning learns from examples with right answers (labels). Clustering gets no labels; it groups points that are close to each other.
02Two centres sit at c1 = (1, 1) and c2 = (5, 5). Which centre does the point (2, 3) join in the assign step?
Distance to c1: √((2 − 1)² + (3 − 1)²) = √5 ≈ 2.24. Distance to c2: √(3² + 2²) = √13 ≈ 3.61. Each point joins its nearest centre.
03A group holds the points (2, 4), (4, 4) and (6, 1). Where does its centre move in the update step?
The centre moves to the mean: x = (2 + 4 + 6) / 3 = 4 and y = (4 + 4 + 1) / 3 = 3, so (4, 3).
04When does k-means stop?
If no point switches centre, the means are the same as before, so the next pass would change nothing either. Neither step can raise the inertia, and there are only finitely many groupings, so this always happens.
05The best inertia for k = 1 to 5 is 400, 90, 70, 60, 52. Which k does the elbow method suggest?
The inertia drops sharply from k = 1 to k = 2, then only a little after that: the elbow is at k = 2. The lowest inertia always comes from the largest k, so that is not the goal.
Glossary
- Clustering
- Grouping data points so that points in the same group are similar (close) and points in different groups are not. It needs no labels.
- Unsupervised learning
- Learning from data with no right answers (labels) attached, for example finding groups or structure.
- k-means
- A clustering algorithm: pick k centres, assign every point to its nearest centre, move each centre to the mean of its points, and repeat until nothing changes.
- Centre (centroid)
- The point that stands for a group. In k-means it is the mean (average) of the group's points.
- Euclidean distance
- The straight-line distance between two points: √((x1 − x2)² + (y1 − y2)²). Python: math.dist(p, q).
- Inertia
- The sum of the squared distances from every point to its own centre. Lower means tighter groups; k-means never raises it.
- Elbow method
- A way to choose k: plot the inertia for k = 1, 2, 3, … and pick the k where it stops falling fast.
- k-means++
- A way to choose the starting centres that spreads them apart. It is scikit-learn's default and usually leads to better groups.
Notes and sources
- The Python program in the lesson is real: run it and it prints [(2.0, 2.0), (7.0, 7.0)] then [0, 0, 0, 1, 1, 1].
- Distance mixes the features, so features in different units are usually scaled first (for example to mean 0 and standard deviation 1); otherwise the feature with the biggest numbers decides the groups.
- The elbow numbers are the best inertia over every possible split of the six customers into k groups, found by checking them all. On real data you run k-means for each k and plot its inertia.
- k-means suits round, similar-sized groups. For long, curved or uneven groups other methods (such as DBSCAN or hierarchical clustering) do better.
- Stanford CS229 Machine Learning, lecture notes (A. Ng, T. Ma): the k-means clustering algorithm and its distortion function
- S. P. Lloyd, Least squares quantization in PCM, IEEE Transactions on Information Theory 28(2), 1982
- D. Arthur and S. Vassilvitskii, k-means++: The Advantages of Careful Seeding, SODA 2007
- scikit-learn User Guide: Clustering, K-means (inertia, local minima, k-means++)
- scikit-learn API: sklearn.cluster.KMeans (n_clusters, init, n_init, inertia_)
- C. M. Bishop, Pattern Recognition and Machine Learning (2006), section 9.1: K-means clustering
- Google for Developers, Clustering course: what clustering is and k-means
- Python Library Reference: math.dist
Quiz Unlocks after the last step.