Lesson 6.3 · 11 min read · about 1 h to master at a very slow pace

Updating Centroids and Convergence

The update step, why K-means always converges but only to a local optimum, the effect of a bad start, choosing K with the elbow method, and the main ways K-means goes wrong.

Not startedJump to quiz

The update step, continued

From 6.2: , . Move each centroid to its cluster mean:

Iteration 2: reassign. Every point is still closest to the same centroid, so assignments don't change: converged.

.

Why it converges

  • The assignment step can't increase : each point moves to a closer (or equally close) centre.
  • The update step can't increase : the mean minimises the sum of squared distances within a cluster.
  • There are finitely many ways to assign points, so can't keep decreasing forever.

But it converges to a local minimum, which depends on the starting centroids.

Worked example: a poor start that recovers

1-D points ; ; start at , .

  1. Assign: 1 → ; 2, 4, 11, 12, 14 → . Update: , .
  2. Assign: 1, 2 (1 vs 6.6), 4 (3 vs 4.6) → ; 11, 12, 14 → . Update: , .
  3. Assign: no change. Converged. .

Here a bad start still found the natural groups. On other data it can get stuck in a poor solution, so K-means is run several times from different starts and the run with the lowest is kept. k-means++ chooses spread-out starting centroids to make bad starts rarer.

Click through the same example, one assign or update at a time:

Choosing K in K-means

always decreases as grows (with , every point is its own cluster and ), so you can't choose by minimising , just as training error can't choose model complexity.

025507510012345678number of clusters KWCSS Jelbow at K = 3
Elbow method: big drops up to K = 3, then only small gains. Choose the elbow.
  • Elbow method: plot against and choose the bend where extra clusters stop helping much.
  • Silhouette score: for each point, , where is the mean distance to its own cluster and to the nearest other cluster. Higher average is better.
  • Usefulness and stability: are the clusters actionable, and do they stay similar on resampled data?

When K-means goes wrong

IssueWhyWhat to do
Features on different scalesLarge-scale variable dominates distanceStandardise first
Sensitive to initialisationOnly a local optimumMany restarts; k-means++; keep lowest
must be chosenNot learnedElbow, silhouette, domain use
Assumes round, similar-sized clustersEuclidean distance to a meanOther methods (mixtures, density-based) for elongated or nested clusters
OutliersMeans are pulled by extremesHandle outliers; consider k-medians
Hard assignments onlyEach point fully in one clusterSoft clustering (mixture models)

Cheatsheet

Choosing K

Not by min (falls with K). Use the elbow, silhouette, stability and usefulness.

K-means failure modes

Unscaled features · bad starts (local optima) · wrong K · non-round clusters · outliers · categorical data.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1calculate1-D points 1, 3, 8, 10 with and initial centroids 1 and 10. What are the final centroids and ?
Q2calculate2-D points , initial centroids and . What are the final centroids?
Q3interpretWCSS for to is 100, 45, 22, 19, 17, 16. Which would you choose?
Q4conceptWhy is K-means usually run several times from different random starts?
Q5conceptWhy can't you choose by finding the with the lowest ?