How k-means clustering works

A step-by-step look at the simple loop that sorts data into k groups, why it can settle on the wrong answer, and how to choose k.

Written by Amili, an AI writer, from the sources listed below · 12 October 2026 · 5 min read


K-means clustering is a method that splits a set of data points into k groups, each built around an average point called a centroid. It repeats two simple steps until nothing changes. It matters because it is fast, easy to explain and among the most widely used ways to find structure in unlabelled data.

In short

  • You choose k, the number of groups, before the algorithm starts.
  • Each round assigns every point to its nearest centroid, then moves each centroid to the mean of its points.
  • The loop always settles, but often on a local best rather than the best possible grouping.
  • Results depend on the starting centroids, so it is usually run several times.
  • It favours round groups of similar size and struggles with other shapes.

What problem does k-means solve?


Suppose you have a pile of measurements with no labels: customers described by how often and how much they buy, or pixels described by their colour. You suspect the data fall into a few natural groups, but nobody has told you what they are. Finding them is called clustering, and k-means is the best known way to do it.

The method asks for one number up front, k, the number of groups you want. It then looks for k centre points so that every data point sits close to the centre of its own group. Formally it tries to make the total of the squared distances between each point and its group centre as small as possible. Grouping by squared distance is what makes the average the natural choice of centre.

How does the algorithm work, step by step?


The standard version, often called Lloyd's algorithm, starts with k initial centroids and then alternates between two steps.

In the assignment step, every point joins the group whose centroid is closest. In the update step, each centroid moves to the average position of the points now in its group. Then the algorithm assigns again with the new centroids, updates again, and so on.

Each round can only lower the total squared distance or leave it unchanged, so the process must eventually stop. It has converged when a full round passes and no point changes group.

A worked example with six numbers


Take six values on a line: 1, 2, 3, 10, 11 and 12, and ask for two groups. Start, unwisely, with the centroids at 1 and 2.

Assignment: 1 is nearest the first centroid; every other value is nearest 2. Update: the first centroid stays at 1, and the second moves to the average of 2, 3, 10, 11 and 12, which is 7.6.

Assignment again: 2 and 3 are now closer to 1 than to 7.6, so they switch groups, while 10, 11 and 12 stay. Update: the first centroid moves to 2, the average of 1, 2 and 3, and the second moves to 11. One more round changes nothing, so the algorithm stops with the two obvious groups. A different starting point on messier data could have stopped somewhere worse.

Where is it used?


The idea has several roots. Hugo Steinhaus described it in 1956, Stuart Lloyd at Bell Labs proposed the standard algorithm in 1957 for pulse-code modulation, though his paper only appeared in a journal in 1982, and James MacQueen coined the name k-means in 1967. Edward Forgy published essentially the same method in 1965.

Its first uses were in signal processing and data compression, where a large range of values is represented by a small set of typical ones. As computers grew faster it spread to pattern recognition, statistical classification and machine learning, largely because it is simple and quick. Clustering in general is used in image analysis, information retrieval, bioinformatics and computer graphics.

Where does it fail?


Finding the truly best grouping is computationally hard, so k-means settles for a good local answer. Which answer it reaches depends on where the centroids begin. Two common starting rules are Forgy, which picks k real data points at random, and Random Partition, which assigns points to groups at random first. Because a single run is fast, practitioners usually run it many times and keep the best. A smarter starting rule, k-means++, comes with a guaranteed bound on how bad the result can be.

Choosing k is a second weak spot. The popular elbow method, which looks for a bend in a chart of fit against k, is known to be unreliable; measures such as the silhouette score or the gap statistic are alternatives.

Finally, k-means tends to find compact groups of roughly equal spread. Long, curved or very uneven clusters confuse it. Gaussian mixture models allow clusters of different shapes, and k-medians or k-medoids are less tied to squared distance.

What does it teach about thinking?


K-means will always return k groups, whether or not the data really contain them. The structure it reports is partly a product of the question you asked. A well-known line in the field puts it neatly: a cluster lies in the eye of the beholder.

It is also a clean illustration of a local optimum. A sensible process, improving step by step, can stop at a respectable answer that is not the best one. Starting from several places is a cheap defence, in clustering and elsewhere.

Questions people ask


Is k-means the same as k-nearest neighbours?

No, despite the similar name. K-means is unsupervised: it finds groups in data that have no labels. K-nearest neighbours is a supervised method that classifies a new point by looking at labelled examples close to it. They do connect, though: once k-means has found centroids, a new point can be placed in the group whose centroid is nearest.

How do you choose the number of clusters?

There is no single correct method. The elbow method plots how well the groups fit as k grows and looks for a bend, but that bend is often unclear. The silhouette score checks whether points sit closer to their own group than to neighbouring ones, and the gap statistic compares the fit with what random data would give.

Does k-means always find the best clustering?

No. It always stops, because each round can only improve or hold steady the total squared distance, but it can stop at a local optimum. The outcome depends on the starting centroids, which is why it is usually run several times from different starts, or started with a careful rule such as k-means++.

The thinking behind it


Its tour of machine learning includes clustering among the main ways computers learn from data without being told the answers.

Read or listen to The Master Algorithm

Hear the whole book free: start an Audible trial and your first audiobook — this one, if you like — is on the house.

As an Amazon Associate, ReadGlobe earns from qualifying purchases and Audible trials — at no extra cost to you.

Sources

How this was made: Amili, an AI writer, wrote this article in its own words from the sources above. Every link was checked before publishing. Spotted an error? Tell us and we will correct it.

More algorithms, explained


Books readers reach for

As an Amazon Associate, ReadGlobe earns from qualifying purchases — at no extra cost to you.