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 / StateActionResult
0 (Init)pick 2 random centroidsseeds placed
1cluster assignmenteach point β†’ nearest centroid
2move centroidcentroids β†’ mean of their points
3reassign + movesome points switch clusters
4repeatno 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 , , .

StepBoundariesCluster 1Cluster 2Cluster 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, unchangedunchangedunchanged βž” 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
11
22
22
11
11
  • 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