Clustering, and What the Objective Assumes
What you will be able to do
Given a small dataset and an initialisation, the learner can carry out k-means assignment and update steps to convergence, and can recognise that the returned partition is locally optimal.
What you will be able to do
Given a clustering result, the learner can read a within-cluster sum of squares curve without inferring a cluster count the data does not carry, and can attribute a failure to the objective's notion of a cluster rather than to the search.
Orientation
When the objective and the intended grouping disagree
Thirty-six points lie on two concentric rings, twelve on an inner circle, twenty-four on an outer one. Anyone looking at them sees two groups.
Run k-means with
The instinct is that the algorithm converged badly and more restarts would fix it. That instinct is wrong, and checking it is what this unit is about. The half-moon partition scores
The answer you wanted is worse by the criterion you asked for. Nothing was broken.
This matters because clustering has no test set. There is no response variable, nothing held out, no error to estimate, so the usual way of finding out that a model is wrong is unavailable. What replaces it is knowing which partitions the criterion scores well, and this criterion scores points sitting close to a centre. The mean of a ring falls in the hole.
Two further things about k-means are worth the same scrutiny. It stops at a partition no single point can improve, which is not the best partition: on a nine-point dataset in this unit, a quarter of random starts converge to an answer eighteen times worse than the optimum. And the curve usually used to choose the number of groups falls monotonically whatever the data, so on data with no separated groups it still produces a plot, and reading a
This unit covers the procedure, what convergence guarantees, how to tell a real elbow from a smooth decay, and how to recognise when the objective rather than the algorithm is the thing that failed.
Definition
What each part of the definition is load-bearing for
The canonical statement above gives the objective and the two steps. What follows is what each piece commits you to, since that is what decides how a result should be read.
The objective is a choice, and it is the whole of the method's opinion. Everything k-means believes about what a group is lives in
The two steps are a search procedure for minimising it; swap in a different criterion and you have a different method, not a different implementation. So when a result looks wrong, the question splits: did the search fail, or was the criterion never going to reward the answer you wanted? These have different fixes and are diagnosed differently.
Why both steps only ever lower the objective. Assignment moves a point to a nearer centroid, which cannot raise its squared distance term. Update moves a centroid to the mean of its members, and the mean is the unique minimiser of
| The claim | What it rests on | What it does not give |
|---|---|---|
| the algorithm terminates | finitely many assignments, objective never rises | no bound on how good the result is |
| convergence reached | no point improves alone, every centroid at its mean | not the global optimum |
| WCSS falls as | more groups fit more closely, always | no evidence about the right |
| a group was found | the criterion was locally satisfied | not that the group means anything |
What convergence certifies, exactly. Two conditions hold at the end: no single point lowers the objective by changing groups, and each centroid sits at its members' mean. Both are local, statements about single moves. A partition can satisfy both and still be far from optimal, because escaping would require several points to move together, which the algorithm has no mechanism to do.
Distance is not neutral. Squared Euclidean distance makes the objective scale-dependent: rescale one coordinate and a different partition minimises WCSS. Whether to standardise is therefore a decision about how much each variable should count, taken before clustering and never recoverable from the output.
Intuition
Three failure modes of k-means
Each iteration assigns every point to its nearest centroid and then moves each centroid to the mean of its members. Both steps decrease the within-cluster sum of squares or leave it unchanged, and the number of possible assignments is finite, so the algorithm terminates.
Termination is a weaker guarantee than it appears, and three distinct things can go wrong.
Convergence to a local optimum. The algorithm stops when no single point lowers the objective by changing clusters. That condition can hold at a partition far from the best one. On the nine-point dataset in this unit, an initialisation placing two centroids inside one tight group converges to WCSS
Choosing
Mismatch between the objective and the cluster structure. Minimising squared distance to a cluster mean favours clusters that are compact around a centre. For a ring of points the mean lies at the centre of the ring, where no data is, so every point is far from it. On the two concentric rings in this unit, k-means with
The three modes are distinguishable by evidence. If the intended partition scores better than the returned one, the search failed and restarts are the remedy. If it scores worse, the criterion does not match the structure and a different method is required.
Example
Four datasets and what the objective says about each
Four well-separated groups,
| WCSS | drop | |
|---|---|---|
| 1 | — | |
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 |
The drop at
One elongated blob, no
No cliff. The reductions decay smoothly, and the last one is larger than the one before it, so the sequence is not even monotone. The curve identifies no number of groups, because there are none to identify. A reader determined to find an elbow could point at
Two concentric rings, where the criterion favours a different partition. Twelve points at radius
The method is not failing to find the rings. It is rejecting them, correctly, under the criterion it was given.
The same rings under a different criterion. Single linkage merges the two groups whose nearest members are closest. Applied to the same thirty-six points with
---
The first is the case the elbow method is sold on, and it works. The second shows the same plot produced from data with no structure, which is why the plot alone is not evidence. The third shows the objective preferring an answer the analyst does not want, which no amount of computation will overturn. The fourth shows that the disagreement was never about the data. Two criteria, two different right answers.
Procedure
Running it, and checking what the run established
To run k-means on a small dataset by hand.
- Decide on scaling first. Squared Euclidean distance treats a unit of one variable as equal to a unit of another. If the variables are in different units, standardise, and record that you did, because the partition depends on it.
- Choose
and place the initial centroids. Record where they started. A result is not reproducible or diagnosable without this. - Assign. Compute each point's squared distance to every centroid; give it the label of the nearest.
- Update. Move each centroid to the coordinatewise mean of its assigned points.
- Repeat from step 3 until the labels stop changing. The stopping condition is label stability, not a fixed iteration count, stopping at a count leaves you unable to say whether the result is a converged partition or a snapshot of one still moving.
- Compute the final WCSS and report it alongside the partition. A partition without its objective value cannot be compared with anything.
To establish that a solution is worth reporting.
- Restart. Run steps 2–6 several times from different initialisations.
- Compare by objective value, not by appearance. The solution with the lowest WCSS is the best one found; how reasonable a partition looks is not the criterion the method used.
- Report the best and say how many restarts produced it. "Lowest WCSS over 50 restarts" is a claim a reader can weigh; "k-means gave this" is not.
- If the restarts disagree substantially, say so. A wide spread of objective values across starts is information about the data, it means the landscape has distinct basins, and suppressing it overstates how determinate the answer is.
To read a WCSS curve.
- Compute the best WCSS at each
, each from multiple restarts, so the curve reflects the objective rather than the luck of initialisation. - Tabulate the successive drops, in absolute terms and as a percentage of the previous value.
- Look for a discontinuity in the drops, not a bend in the curve. A curve always slopes downwards; what distinguishes a real elbow is a large drop followed by a much smaller one.
- If the drops decay smoothly, name no
. That is the finding. Reporting a number anyway attributes structure to data that does not show it.
To diagnose a result that disagrees with what you see.
- Compute the objective value of the partition you expected, and compare it with the one returned.
- If yours scores worse, the search did not fail. The returned answer scores better on the criterion, and the mismatch is between the criterion and your notion of a group. Restarts will not help.
- If yours scores better, the search did fail, and more restarts are exactly the fix.
- Only then consider a different method, choosing by what its criterion scores rather than by reputation.
Checks. Confirm each centroid lies at the mean of its own members, if not, the update step was applied incorrectly or the run has not converged. Confirm the objective fell at every step; a rise means an arithmetic error, since neither step can raise it. And before reporting a cluster count, ask what the drop sequence would look like if the data had no groups at all: if you cannot say how that would differ from what you are looking at, the curve is not evidence.
Worked example
One dataset, two initialisations, an eighteenfold gap
The data. Nine points in two dimensions: two groups of three close together on the left, one group of three far away on the right.
| Group | Points |
|---|---|
| L1 | |
| L2 | |
| R |
We want
The optimum, established by enumeration. Rather than trusting the algorithm, enumerate every way of splitting nine points into three non-empty groups, compute each partition's WCSS exactly, and take the minimum. The best partition has L1, L2 and R each forming a cluster, with
This is the benchmark. Every result below is compared against it, so "worse" means worse than a verified optimum rather than worse than another run.
Run 1: one seed per group. Start the centroids at
Assignment. Each point is nearest the seed in its own group. The between-group gaps (
Update. Each centroid moves to its group's mean:
Assignment again. No label changes. Converged.
Run 2: two seeds inside the far group. Start at
Assignment. The two right-hand seeds split group R between them. The single left seed at
Update. The left centroid moves to the mean of all six left points,
Assignment again. The left points remain nearest that centroid; the right points remain with the two right centroids. Labels settle as
Both runs converged. In each, no point improves by moving alone and every centroid sits at its members' mean. The definition of convergence is met identically. Yet
so the second answer is eighteen times worse on the very quantity being minimised.
Why run 2 cannot escape. To reach the optimum, one of the two right-hand centroids would have to relocate to the left region. It moves only to the mean of its assigned points, and its assigned points are all on the right. Meanwhile no left point can improve by switching, since both alternatives are far away. The partition is locally unimprovable and globally poor, and the algorithm has no mechanism to notice.
How often this happens. Running k-means from
A quarter. Not an edge case.
What detects it. Nothing inside a single run. The objective value of one converged solution carries no information about whether a better one exists,
Contrast
Pairs that differ in one respect
A bad search against a bad criterion.
| nine points, run 2 | concentric rings | |
|---|---|---|
| returned WCSS | ||
| intended partition's WCSS | ||
| intended answer scores | better | worse |
| diagnosis | search stopped early | criterion rejects it |
| fix | restarts | different criterion |
Both look identical from the outside: a result that disagrees with what you expected. The single computation that separates them is the objective value of the partition you wanted. If it beats what was returned, the search failed. If it loses, the search succeeded and the criterion is the thing that does not match.
The two failures have opposite remedies, and restarts address only the first.
A sharp elbow against a smooth decay.
Four separated groups: drops of
Both curves fall monotonically; both can be plotted and pointed at. Only the first contains a discontinuity, and it is the discontinuity rather than the downward slope that carries the information. Note that the blob's first drop,
k-means against single linkage, on identical data.
The thirty-six ring points,
Neither is the true clustering. What single linkage gains in recovering elongated structure it pays for in fragility: a single point between the rings would chain them into one cluster, which is a failure mode k-means does not have. Neither method dominates.
Convergence against optimality.
Run 1 and run 2 on the nine points both satisfy the stopping condition exactly. No point improves alone, every centroid at its members' mean. One scores
A number of groups the data supports, against a number you supplied.
k-means with
Warning
Reporting errors in a clustering result
Reporting a single run. One converged solution carries no information about whether a better one exists:
Reading a
Treating a disagreement with the eye as an algorithmic failure. When the returned partition differs from the expected one, compute the expected partition's objective value before concluding anything. On the rings it scores
Clustering unstandardised variables without saying so. Squared Euclidean distance makes income in euros and age in years directly comparable, which they are not. Rescaling one variable changes which partition minimises the objective, so the partition is partly a consequence of the units the data happened to arrive in. Standardising is also a choice, it asserts every variable should count equally, and that choice belongs in the report.
Interpreting clusters as kinds. The method partitions into exactly the
---
Two that follow from having no test set.
Looking for held-out validation that does not exist. Nothing was predicted, so there is no error to estimate and no split that would settle the question. The discipline that replaces it is knowing which partitions the criterion scores well and checking whether that matches the intended notion of a group, which is reasoning done before and after the fit, not a number computed from it.
Taking stability for correctness. A partition that reappears across restarts is reliably found, which is a fact about the objective's landscape rather than about the data's structure. The rings partition is perfectly stable: k-means returns the same
---
And one about the status of the output. Clustering is an exploratory move that proposes groups for a human to interpret, and every step of it, the choice of
Application
Where the clustering criterion determines the outcome
Customer segmentation. The commercial question is which customers to treat alike, and k-means on standardised spending and frequency variables gives a workable answer, because segments defined by "similar on these measures" are exactly blobs around a centre. What the method cannot supply is
Vector quantisation and image compression. Here the criterion is not a proxy for anything. The goal genuinely is to replace each observation with a representative minimising squared error, which is what WCSS measures. Clusters need not correspond to any real category, and nobody interprets them. This is the case where k-means is not an approximation to a better method; it is the method, and the local-optimum problem is handled exactly as this unit prescribes, by restarts compared on objective value.
Document and gene-expression grouping, where the metric is the decision. Squared Euclidean distance on raw counts makes long documents and highly expressed genes dominate, so the substantive work is choosing a representation and distance, cosine similarity, correlation distance, before any clustering runs. The choice determines the answer more than the algorithm does, and it is made on subject-matter grounds that the data cannot settle.
Anomaly detection, where the ring failure is the point. Normal behaviour often occupies a shell or manifold rather than a ball, network traffic within operating bounds, sensor readings on a cycle. A centroid-based method placing a centre in the middle of that shell will score genuinely anomalous points near the centre as typical, since they are close to the mean. This is the concentric-rings failure with consequences, and it is the standard argument for density- and connectivity-based methods in this setting.
Preprocessing for a supervised model. Cluster labels used as features inherit every choice above, and the danger is specific: clustering the full dataset before splitting lets information from the held-out rows influence the features, so the later error estimate is optimistic. The clustering must be fitted on the training split alone and applied to the rest. The same discipline the evaluation unit establishes, applied to a step that does not look like model fitting.
---
In each case the analyst supplies the notion of a group, through