k-means Clustering
Context: FIT1043_MOC, FIT2086_MOC, FIT3003_MOC Β· the flagship unsupervised (no-label) method Β· groups points by similarity Β· partitions data into clusters Β· density-based rival β DBSCAN (Density-Based Clustering) Β· probabilistic, soft-assignment generalisation β Mixture Models (EM Algorithm) (k-means vs mixture matrix lives there)
Quick Revision
- π― Objective: group unlabelled points into clusters by similarity β iterate assign β move-centroid until stable β this minimises the within-cluster sum of squares .
- π¦ Core Components: = number of clusters | centroid = mean of a clusterβs points | two repeating steps.
- β‘ Key Constraint: the initial random centroids matter β poor initialisation gives volatile, different results and only a local minimum of ; must be chosen up front.
π How It Works
1. Basics
- Clustering β grouping a set of data points into subgroups (clusters) based on similarity (unsupervised; e.g. T-shirt sizes S/M/L β ).
- β the number of clusters (chosen before running).
- Centroid β the mean (average) location of all points in a cluster.
2. The Two Iterative Steps
- 1. Cluster assignment β assign each point to its nearest centroid.
- 2. Move centroid β move each centroid to the mean of the points now assigned to it.
- Stop β repeat until no change (assignments/centroids stabilise).
3. Initialisation & Choosing
- Random init β pick random data points as starting centroids; highly volatile β poorly positioned seeds give poor/different clusterings.
- Choosing β a priori domain knowledge ( two kinds of people; bacteria types; T-shirt sizes); search (try several , evaluate quality); or run hierarchical clustering on a subset.
4. Clustering Fact Measures (FIT3003)
- Partition β objects are mutually exclusively assigned to the predefined clusters; each object goes to its nearest centroid.
- Distance β Euclidean over measures: .
- 1-D boundaries β sorted values split at the midpoints of adjacent centroids β no per-point distances needed.
- Non-uniform units β Glucose spans β, Albumin β: stretching either axis changes the clusters and the wider measure dominates the distance β normalise each measure to a common scale (β) first.
- 2-D boundaries β a Voronoi diagram β each region holds the points nearest one centroid; recompute centroids from members, redraw, repeat.
- Termination β stop when no member moves between clusters (a centroid can shift while its members stay put: ).
5. The Objective & Convergence (FIT2086)
- Data β items, each a point in -dimensional space; = distance from to centroid .
- Objective β β squared distance of every point to its nearest centroid = total within-cluster sum of squares (WCSS).
- Algorithm in this notation β initialise randomly β loop: compute every Β· assign to the closest centroid Β· re-estimate each from its points β until convergence.
- Converges quickly to a stable solution β but possibly a local minimum, not the lowest possible WCSS; sensitive to starting points.
- k-means++ β choose each new starting centre at random with probability proportional to its squared distance from the nearest centre already chosen βΉ seeds spread out βΉ convergence improves dramatically.
6. Other Distances & Limitations (FIT2086)
- Alternative similarity measures β weighted Euclidean Β· βcityblockβ () Β· Hamming (pure binary data) Β· many more.
- Limitations β hard classification of items to clusters Β· mixed continuous/discrete attributes are difficult Β· no explicit statistical interpretation Β· no principled way to choose from the data alone βΉ mixture modelling is the flexible alternative.
π Exam Execution Trace
Manual Execution Trace
k-means with :
| Step / State | Action | Result |
|---|---|---|
| 0 (Init) | pick 2 random centroids | seeds placed |
| 1 | cluster assignment | each point β nearest centroid |
| 2 | move centroid | centroids β mean of their points |
| 3 | reassign + move | some points switch clusters |
| 4 | repeat | no change β converged |
Final Extracted Output: stable clusters, each summarised by its centroid (the mean of its members).
Applied Exercise (FIT3003 β 1-D Glucose, )
Problem: , initial , , .
| Step | Boundaries | Cluster 1 | Cluster 2 | Cluster 3 | |
|---|---|---|---|---|---|
| 1 | , | 64 68 70 72 91 93 99 121 145 154 154 162 (, ) | 169 180 () | 198 200 218 () | |
| 2 | , | 64 β¦ 121 (, ) | 145 154 154 162 169 180 () | unchanged | |
| 3 | , | unchanged | unchanged | unchanged β stop |
Final Extracted Output: , , with centroids , , .
Applied Exercise (FIT2086 β 2-D height/weight, )
Problem: , , , , (cm, kg); seeds , .
| Point | to | to | Iter 1 | to | to | Iter 2 |
|---|---|---|---|---|---|---|
| 1 | 1 | |||||
| 2 | 2 | |||||
| 2 | 2 | |||||
| 1 | 1 | |||||
| 1 | 1 |
- Re-centre after iter 1 β Β· .
- WCSS β at the seeds β after one update; iter 2 reassigns nobody βΉ means unchanged βΉ stop.
Final Extracted Output: Cluster 1 at , Cluster 2 at .
β οΈ Common Mistakes
- π‘ Different seeds β different clusters β random initialisation is volatile and converges to a local minimum of WCSS; rerun from several seeds or use k-means++.
- π‘ Clustering raw, unnormalised measures β the wide-range measure decides every assignment; normalise first (FIT3003 Glucose vs Albumin).
- π‘ You must pick β k-means canβt discover the number of clusters; use domain knowledge or search over .
π§ Active Recall
State the two iterative steps of k-means and its stopping condition.
- Hint: Assign then re-centre.
Answer
- Short answer: (1) Cluster assignment β assign each point to the nearest centroid; (2) Move centroid β set each centroid to the mean of its assigned points; repeat until no change.
- Why: Centroid = mean β each pass lowers within-cluster distance until assignments stabilise.
Why does the initial choice of centroids matter, and how do you choose ?
- Hint: Volatile init + preset k.
Answer
- Short answer: Random seeds are volatile and can converge to poor clusterings; choose from domain knowledge, by searching over values, or via hierarchical clustering on a subset.
- Why: Seed sensitivity β k-means finds a local solution, so starting points shape the outcome.
Centroids , . Which cluster gets ? (FIT2086 lecture Q1)
Answer
- Short answer: cluster 1 β squared distances vs .
- Why: Hard assignment β the point goes wholly to the nearer centroid; a mixture model would instead report a probability for each class.
What does k-means minimise, and why can "converged" still be a bad answer?
Answer
- Short answer: the within-cluster sum of squares ; the algorithm stops at a stable solution, which may be a local minimum determined by the seeds.
- Why: Non-convex objective β different seeds lead to different stable partitions; k-means++ spreads the seeds (probability squared distance to the nearest chosen centre) to reach better minima.