Foundations to Frontier
Tests

k-Means++ Initialization

k-means++ chooses starting centres for Lloyd's algorithm so that they are spread out. The first centre is a random data point; each next centre is drawn with probability proportional to D², the squared distance to the nearest centre already chosen. This one-pass seeding already gives a solution within an O(log k) factor of optimal in expectation, and Lloyd's algorithm then improves it further. Greedy k-means++, the scikit-learn default, draws a few candidates per step and keeps the best. This page covers D² sampling, the approximation guarantee, the greedy variant, cost, and scalable k-means||. In the exercises you implement both seedings and compare them with random starts; then you measure final quality, success rate and Lloyd iterations as k grows.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Start with centres far apart

Lloyd’s algorithm gets stuck when two starting centres land in the same group. k-means++ avoids that by choosing starts that are far apart, at random, but with a bias:

  • the first centre is any data point;
  • each next centre is picked with probability proportional to its squared distance to the nearest centre already chosen.

Points near an existing centre are almost never picked, so the next centre usually lands in a group that has none yet.

k-means++: spread the starting centres out
60 seedingsinertia ÷ optimumwithin 1% of optimum
random data points2.03322%
k-means++1.59547%
greedy k-means++1.001100%
seeds 1/9 · blobs with a seed 1 · seeding cost ÷ optimum 65.14
Dot size is the chance each point becomes the next seed under D² sampling. Points in blobs that already hold a seed are nearly invisible, so the next seed almost always lands in an empty blob. Random seeding ignores this and often doubles up. Greedy k-means++ tries 4 candidates per step and keeps the best, which is what scikit-learn does by default.
Why engineers care

Bad initialisation is the main reason k-means gives poor or unrepeatable clusters. k-means++ is cheap (one pass over the data per centre), is the default in scikit-learn, Spark MLlib and most libraries, and makes results far more consistent across runs. That matters when cluster labels feed downstream models or reports.

📝 Quick check (Level 0)
0/3 answered
  1. 1. k-means++ prefers new centres that are…
  2. 2. A point sitting on an existing centre has selection probability…
  3. 3. In the demo, random seeding often…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

D² sampling by hand

Points 0, 1, 3, 10 with the first centre at 0. The squared distances are 0, 1, 9, 100, total 110. The next centre is

  • 10 with probability 100/110 = 0.909;
  • 3 with probability 9/110;
  • 1 with probability 1/110.

After two centres at 0 and 10, D² for the point 1 is min(1, 81) = 1, and for 3 it is min(9, 49) = 9.

Greedy variant. Draw L = 2 + ⌊ln k⌋ candidates per step and keep the one that lowers the total D² most. For k = 9, L = 4. For k = 1000, L = 8.

Cost. Each new centre needs one pass to update D², so seeding costs O(nkd) for plain k-means++ and L times that for greedy. That is about the cost of one Lloyd iteration (greedy: a few), and it usually saves several iterations.

The steps in plain English:

  1. Pick the first centre uniformly at random from the data.
  2. For every point, compute D², the squared distance to the nearest chosen centre.
  3. Pick the next centre with probability D² / ΣD². (Greedy: draw L candidates and keep the one that reduces ΣD² most.)
  4. Repeat 2–3 until there are k centres, then run Lloyd’s algorithm.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Points 0, 1, 3 with a centre at 0: probability that 3 is next?
  2. 2. Greedy candidates for k = 25?
  3. 3. Seeding cost of plain k-means++?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

Why spreading helps, and how much

The failure it fixes. Random seeding picks k points uniformly, so blobs receive seeds in proportion to their size. With k equal blobs, the chance that every blob gets exactly one seed is k!/kᵏ, already under 0.1% for k = 9. Doubled-up seeds lead to the local minima seen in the previous topic.

D² sampling makes a seed in an already-covered blob unlikely, because those points have small D² compared with points in empty blobs. Exercise 1 (16 grid blobs, 40 seedings each):

seeding seeds alone ÷ optimum after Lloyd ÷ optimum optimum reached blobs with exactly one seed
random 7.03 2.266 2% 6.2 / 16
k-means++ 3.18 1.670 18% 12.2 / 16
greedy k-means++ 1.99 1.118 80% 15.2 / 16

Plain k-means++ still misses often. D² sampling is random, so one blob in a few still gets two seeds. Greedy candidates fix most of these mistakes, which is why scikit-learn uses them.

Outliers. D² sampling is attracted to far-away points. A few gross outliers can each grab a seed. Clean the data first, or use a robust variant.

k-means|| (scalable k-means++). It oversamples about ℓ ≈ 2k candidates per round for O(log n) rounds in parallel, then reclusters the weighted candidates down to k. Its quality is similar to k-means++ in a few distributed passes (Bahmani et al., 2012).

from sklearn.cluster import KMeans, kmeans_plusplus
centres, idx = kmeans_plusplus(X, n_clusters=9, random_state=0)       # greedy D² seeding
km = KMeans(n_clusters=9, init="k-means++", n_init=1).fit(X)
🐍 Exercise 1: k-means++ and its greedy variant
Implement random seeding, D² (k-means++) seeding with an optional number of greedy candidates, and Lloyd; on 16 grid blobs compare seeding cost, final cost, success rate and blob coverage.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Probability that k random seeds hit each of k equal blobs once?
  2. 2. D² sampling is sensitive to…
  3. 3. k-means|| is designed for…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Approximation guarantee (Arthur and Vassilvitskii, 2007). Let φ be the k-means cost of the seeds alone, before any Lloyd step. Then

E[φ] ≤ 8(ln k + 2)·OPT.

For k = 16 the bound is 38.2×. Exercise 1 measured 3.18×: the bound is a worst case. Lloyd can only lower the cost further.

Proof idea.

  • If a seed is drawn uniformly from an optimal cluster A, its expected cost on A is 2·OPT(A).
  • Drawing by D² instead costs at most 8·OPT(A), because each point’s D² is close to its distance to the nearest earlier seed.
  • An inductive argument shows that clusters missed by early seeds contribute an extra harmonic-number factor H_k ≈ ln k.

The log k is tight for D² seeding in the worst case. Greedy variants are not covered by the original bound but do better empirically.

Lower iteration counts. Good seeds are near the final centres, so Lloyd needs fewer steps. Exercise 2 at k = 25: random 11.0 iterations, k-means++ 8.2, greedy 4.4.

Interview questions.

  • Why squared distance rather than distance? D² matches the k-means objective, and it gives the O(log k) guarantee. Plain D sampling has weaker guarantees.
  • Does k-means++ remove the need for restarts? No. It reduces them a lot, but one draw can still double up. Use a few restarts or the greedy variant.
📝 Quick check (Level 3)
0/3 answered
  1. 1. The k-means++ bound 8(ln k + 2) for k = 9 is about…
  2. 2. The bound applies to the cost of…
  3. 3. Good seeds make Lloyd converge in…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

How much does seeding matter as k grows?

History.

  • D. Arthur and S. Vassilvitskii introduced k-means++ and its O(log k) guarantee in 2007.
  • R. Ostrovsky, Y. Rabani, L. Schulman and C. Swamy (2006) analysed a similar seeding.
  • B. Bahmani et al. (2012) proposed k-means|| for MapReduce and Spark.
  • scikit-learn adopted greedy k-means++ (2 + ⌊ln k⌋ candidates) as its default seeding.

Exercise 2. Grid blobs, 60 seedings per method. Each cell shows mean inertia ÷ optimum with one start, the expected best of 5 starts, the single-start hit rate and the mean number of Lloyd iterations. Hit rates differ from the previous topic’s table because the data sample differs.

k random k-means++ greedy k-means++
4 1.351, 1.000, 85%, 5.2 1.035, 1.000, 98%, 2.9 1.000, 1.000, 100%, 2.5
9 2.087, 1.461, 13%, 8.1 1.453, 1.014, 57%, 5.4 1.030, 1.000, 97%, 3.1
16 2.209, 1.578, 3%, 9.6 1.568, 1.117, 27%, 7.1 1.047, 1.000, 92%, 3.6
25 2.122, 1.699, 0%, 11.0 1.699, 1.301, 7%, 8.2 1.124, 1.002, 65%, 4.4
  • k-means++ always beat random seeding, but plain D² sampling still faded with k. At k = 25 it reached the optimum in only 7% of single runs.
  • Greedy k-means++ was far stronger. Five greedy starts reached the optimum on average up to k = 16, and came within 0.2% at k = 25. It also needed less than half as many Lloyd iterations as random seeding (4.4 versus 11.0 at k = 25).
🐍 Exercise 2 (research): seeding quality versus k
For k = 4, 9, 16, 25 grid blobs, compare random, k-means++ and greedy k-means++ seeding: mean final inertia ratio, expected best of 5 starts, hit rate, and Lloyd iterations.

k-means++ in AI, robotics and aerospace

  • Codebook learning at scale (visual vocabularies, vector-quantised telemetry) where repeatable centres matter.
  • Initialising GMMs and EM: scikit-learn’s GaussianMixture uses k-means (with k-means++ seeding) to initialise responsibilities by default.
  • Coresets and sampling: D² sampling is the basis of importance sampling for clustering coresets used in streaming and on-board data reduction.

References

  1. D. Arthur, S. Vassilvitskii (2007). “k-means++: the advantages of careful seeding.” Proc. SODA.
  2. B. Bahmani, B. Moseley, A. Vattani, R. Kumar, S. Vassilvitskii (2012). “Scalable k-means++.” Proc. VLDB 5(7).
  3. R. Ostrovsky, Y. Rabani, L. Schulman, C. Swamy (2006). “The effectiveness of Lloyd-type methods for the k-means problem.” Proc. FOCS.
  4. M. E. Celebi, H. A. Kingravi, P. A. Vela (2013). “A comparative study of efficient initialization methods for the k-means clustering algorithm.” Expert Systems with Applications 40.
  5. scikit-learn developers. “sklearn.cluster.kmeans_plusplus.” Documentation.