Visual explainer
K-Means Clustering
Lloyd's nearest-center and mean updates reduce squared error from 113 to 1, but two stable rectangle partitions expose a local-solution limit.
K-means fits K centers by alternating nearest-center assignment and mean updates. Lloyd's algorithm reduces squared error at each exact step, but its result depends on initialization and need not be globally best. The upper diamonds stand for two centers to fit; they are symbolic placeholders, not fitted locations.
Our four synthetic observations are 1,2,9,10. Use K=2 and Euclidean distance. The objective is : observation i has value x_i, assigned group c_i and that group's center μ. K is supplied; it is not discovered by this objective.
Choose Initial Locations
Place centers at 1 and 2. The diamonds sit in separate vertical lanes to keep their labels clear; their horizontal coordinates locate the actual centers. Circles represent observations. Number-line frames use x=70+48 times the data value, so the original distances stay comparable.
The official KMeans API accepts explicit initial centers and supports different seeding strategies. These hand-chosen seeds illustrate a poor starting arrangement. Observations 9 and 10 still receive assignments; they are not orphaned or ignored.
Assign, Then Recenter
The initial midpoint is 1.5. Nearest-center assignment gives 1 and 10, shown open and solid. A tie would go to the first center in our example; none occurs. For fixed centers, choosing each nearest center minimizes that observation's contribution.
Hold these memberships fixed and replace each center with its mean. The first stays at 1. The second becomes (2+9+10)/3=7. The ghost diamond marks its old location 2; the solid diamond marks 7. Its arrow shows the horizontal change, with no intermediate membership changes. The mean minimizes the sum of squared deviations for that fixed group.
Let Assignments Change
Before the update, the boundary was 1.5. Centers 1 and 7 move it to 4. Observation 2 is now distance 1 from center 1 versus distance 5 from center 7, so it switches groups. The ring emphasizes its changed membership.
After this reassignment, means become (1+2)/2=1.5 and (9+10)/2=9.5. Their boundary is 5.5. The next assignment leaves both groups unchanged. This exact fixture reaches a fixed point; practical implementations also have stopping tolerances and iteration caps.
Keep One Error Scale
Each bar uses three SVG units per squared-error unit. The final bar is genuinely only three units long. Read the rows as successive states:
- Initial centers 1,2 and nearest memberships give 0+0+49+64=113.
- Updating to centers 1,7 with those memberships gives 0+25+4+9=38.
- Reassigning observation 2 gives 0+1+4+9=13 at the same centers.
- Updating to 1.5,9.5 gives four deviations of .5, hence 4×.25=1.
Every exact step is non-increasing because it minimizes the objective with the other part held fixed. This does not promise a particular iteration count or convergence rate on other datasets. The algorithm guide describes the same assignment/mean alternation and its dependence on initialization.
K Changes The Representation
Keep the same ruler. K=2 centers at 1.5 and 9.5 give error 1. A valid K=3 solution centers at 1,2,9.5, reducing error to .5. The two left observations now have their own centers and zero residuals.
Singletons are not automatically mistakes. An extra center spends representational capacity to reduce error; whether that helps depends on the intended use and evidence beyond training inertia. For the broader choice of grouping assumptions and feature scaling, see the Clustering visual.
A Boundary Is A Bisector
Switch to a separate two-dimensional fixture: upper points (0,2),(1,3),(2,3),(3,2), lower points (0,0),(1,−1),(2,−1),(3,0). Use 60 SVG units per feature unit in each direction. Candidate centers (.5,1) and (2.5,1) have the exact bisector x=1.5. Open/solid markers now mean left/right assignments; dashed guides show the original curved chains.
Subtracting the two squared-distance expressions cancels x² and y², leaving a linear boundary. Each chain is split by that straight boundary. These candidate centers illustrate the assignment geometry; they are not asserted to be converged means. A centroid objective cannot adapt its bisector to follow every intended curve. Density connectivity is a different assumption, with its own limits.
Assign A Fresh Sample
Return to the fitted one-dimensional centers 1.5 and 9.5 and the original 48-unit ruler. The triangle is a fresh value 8. Its distances are |8−1.5|=6.5 and |8−9.5|=1.5, so it joins the second group.
This prediction holds fitted centers fixed. It does not update them to include the new observation and does not establish a semantic class name for that group. A refit is a separate operation.
Seed With Squared Distance
Consider vanilla k-means++ with the first seed fixed at 1. The next seed's sampling weights for observations 1,2,9,10 are squared distances 0,1,64,81. Normalize by 146: approximately 0,.006849,.438356,.554795. Bars show probabilities at 400 SVG units per probability unit. Observation 1 is omitted from the bars because its weight is zero.
Algorithm 1 in the seeding research paper specifies this squared-distance sampling. The right pair receives probability 145/146 here, while point 2 remains possible. This improves the chance of a spread-out start without guaranteeing an optimum. The official API distinguishes its greedy variant, which tries multiple candidates, from the vanilla sampling illustrated here. No random run or benchmark is claimed.
Where It Breaks
Switch to rectangle corners (0,0),(0,2),(6,0),(6,2) with K=2. Both panels use 35 SVG units per feature unit. Circles are the same four observations; diamonds are means and dashed lines are assignment boundaries. The rectangle outlines locate the fixture and do not assert additional samples.
Group by rows: centers (3,0),(3,2) are the exact means. Each corner is distance 3 from its own center and √13 from the other, so no assignment changes. Error is 4×3²=36. Lloyd's algorithm stops at this stable partition.
Group by columns: centers (0,1),(6,1) are also exact means. Each corner is distance 1 from its own center and √37 from the other. Again no assignment changes, but error is 4×1²=4. The two stable states therefore differ by a factor of nine in objective value.
This concrete counterexample shows that an unchanged assignment is not proof of the globally best solution. Careful initialization and comparing multiple runs can help; neither guarantees that inferred groups match the task's intended meaning.
The Quick Version
- Supply K before fitting the centers.
- Initial centers can start in the same visible pair.
- Means minimize error for fixed memberships.
- Moving centers can change memberships.
- Exact Lloyd steps never increase the squared-error objective.
- More centers can trade complexity for smaller residuals.
- Euclidean equal-distance boundaries are straight.
- Prediction assigns fresh samples without refitting centers.
- K-means++ samples later seeds by squared distance.
- Stable assignments do not guarantee the best solution.