2.5.3 · D3Unsupervised Learning

Worked examples — K-Means++ initialization

2,173 words10 min readBack to topic

This page is a drill hall. The parent note taught you the machinery: pick the first centroid at random, then pick each next one with probability proportional to (the squared distance to the nearest centroid you already have). Here we push that machinery through every kind of situation it can face, so you never meet a case you haven't seen.

Before the first symbol appears, one reminder in plain words:

The recipe we repeat every single time:


The scenario matrix

Every example below is stamped with which cell it fills. Together they cover the whole grid.

Cell What makes it special Example
A Well-separated blobs The clean, textbook case Ex 1
B First pick lands inside a blob Random start biases nothing badly Ex 1
C Two candidates equidistant A tie — how probability splits Ex 2
D Degenerate: duplicate points points, what happens to them Ex 3
E Zero-input edge: all points identical , the division breaks Ex 4
F Outlier present The far-away troublemaker Ex 5
G Limiting behaviour: one point infinitely far Deterministic-in-the-limit Ex 5
H vs head-to-head Why we square Ex 6
I Real-world word problem Store locations on a map Ex 7
J Exam twist: compute after a re-pick Recompute nearest, not first Ex 8

[!example] Example 1 — Cells A & B: clean blobs, first pick inside one

Statement. Six points, two obvious groups: Left group : . Right group : . We already chose . Compute every and the probability of each point being .

Forecast. Guess: which group grabs almost all the probability? (You should feel the right group must win — it is far from .)

Figure — K-Means++ initialization

Step 1 — distance from each point to . Why this step? With only one centroid chosen, "nearest centroid" is , so is just distance to .

point

Step 2 — total . Why this step? The denominator of every probability is the same total, so compute it once.

Step 3 — probabilities. Why this step? Divide each by (the recipe). The left group totals .

Verify. The right group carries of all probability — as forecast, almost surely lands in the far group, exactly the "spread out" behaviour we want. Probabilities sum to : . ✓


[!example] Example 2 — Cell C: a perfect tie

Statement. Points , , and . What is vs ?

Forecast. Guess: symmetric points at equal distance — do they get equal chance? By how much do they beat a third, closer point at if we add one?

Step 1 — squared distances (only ). Why this step? Both sit distance from origin, so each.

Step 2 — probabilities. Why? , so .

Step 3 — add closer point , . Why? To see how a tie survives when a nearer point joins. Now .

Verify. The tie is preserved ( equal) and the near point is less likely than each far point — matching the ratio of their values (). Sum . ✓


[!example] Example 3 — Cell D: duplicate points,

Statement. Points appears twice (a genuine duplicate), plus one point . Take . Can a duplicate of ever become ?

Forecast. Guess: a point sitting exactly on a centroid has distance — what does the recipe do with a ?

Step 1 — squared distances. Why? Both copies of are distance from , so . The point has .

Step 2 — probabilities. Why? .

Step 3 — interpret. Why? A probability of exactly means the duplicate can never be chosen as . Good — you never want two centroids on the same spot.

Verify. Only the far point has non-zero mass and it gets it all: . Duplicates are automatically excluded. Sum . ✓


[!example] Example 4 — Cell E: the degenerate case

Statement. All three points are identical: , and . Try to compute 's probabilities.

Forecast. Guess: if everyone sits on the centroid, what is , and what breaks?

Step 1 — squared distances. Why? Every point equals , so every .

Step 2 — the total. Why? . The recipe wants us to compute undefined. Division by zero is illegal.

Step 3 — the correct handling. Why? When the data has fewer distinct locations than . A real implementation detects and falls back to a uniform random pick (every remaining point equally likely) or stops because more clusters than distinct points is meaningless.

Verify. With requested but only distinct location, no valid second centroid exists; uniform fallback gives each — a legal distribution summing to . ✓


[!example] Example 5 — Cells F & G: an outlier and the limiting case

Statement. Cluster of three points near origin: , plus one outlier . With , find . Then let the outlier drift to and see what happens as .

Forecast. Guess: does the outlier grab nearly all the probability? Does it grab all of it in the limit? (This is the danger the parent note warned about.)

Figure — K-Means++ initialization

Step 1 — squared distances at . Why? Distance to . Inliers: . Outlier: .

Step 2 — probability of . Why? .

Step 3 — the limit . Why this step? To see the degenerate limit. , so As the outlier flies to infinity, its selection probability approaches — the algorithm becomes deterministically forced onto the outlier.

Verify. At : . The limit expression is below for all finite and tends to . ✓


[!example] Example 6 — Cell H: versus head-to-head

Statement. Three candidates with distances (not squared) from the nearest centroid. Compute the pick-probabilities using linear weighting and using squared weighting . Compare.

Forecast. Guess: which scheme gives the farthest point () a bigger share?

Step 1 — linear scheme. Why? Sum the raw distances: .

Step 2 — squared scheme (the real K-Means++). Why? Square first: ; sum .

Step 3 — compare the far point's share. Why? The point rises from to . Squaring sharpens the preference for far points, giving the theoretical guarantee the parent note cites.

Verify. Both sets sum to : and . The squared far-point share linear share. ✓


[!example] Example 7 — Cell I: real-world word problem (store map)

Statement. A chain owns shops at grid coordinates (in km): . They want distribution hubs. The first hub is seeded at . Where is the second hub most likely placed?

Forecast. Guess: which of the two natural clusters (the origin trio or the far pair) should the second hub serve?

Step 1 — squared distances to hub . Why? Only one hub so far; nearest-hub distance = distance to .

shop

Step 2 — total and probabilities. Why? . The nearby trio shares only .

Step 3 — business reading. Why? The far pair carries of the probability, so hub almost surely lands near , serving the distant shops — exactly the spread a logistics planner wants.

Verify. . Far pair total . ✓ Units: coordinates in km, in km², probabilities unitless. ✓


[!example] Example 8 — Cell J: exam twist, recompute after a re-pick

Statement. Points . We already picked two centroids: and . Compute now (the third-round total) — the trap is that each point uses its nearest of the two, not the first.

Forecast. Guess: which centroid is nearest to , and does it matter that there are now two centroids?

Step 1 — nearest centroid for each point. Why this step? The whole twist: is the minimum over all chosen centroids.

  • sits on : distances and → nearest .
  • sits on : distances and → nearest .
  • : to is ; to is — a tie, nearest.

Step 2 — squared distances. Why? .

Step 3 — total and probability. Why? . Then : if we needed a third centroid, is forced.

Verify. from both existing centroids gives ; the two on-centroid points contribute ; and . ✓


[!recall]- Quick self-test

Whole matrix in one place — cover the right side and answer.

Why do points sitting exactly on a chosen centroid have ?
Their , so , and .
What must code do when (all points identical)?
Guard against division by zero and fall back to a uniform random pick (Cell E).
As an outlier's distance , what does its pick-probability approach?
— the limit (Cell G).
Does squaring make the far point more or less likely than linear weighting?
More likely — it sharpens the spread (Ex 6, vs ).
When two centroids exist, which one sets a point's ?
The nearest one — always take the minimum over all chosen centroids (Ex 8).

See also

  • Elbow Method and Silhouette Score — how you judge whether the resulting clusters are good.
  • K-Medoids (PAM) — a cousin that seeds and updates with actual data points, gentler on outliers.
  • Random Seed Setting — because K-Means++ is randomized, fixing the seed makes Ex 1–8 reproducible.
  • Computational Complexity — the cost of recomputing each round (Ex 8's "recompute" step).