What is k-means clustering?
Last updated: 2026-11-23
K-means clustering is a method that sorts a set of data points into k groups, where you choose k, by repeatedly moving each group's center to the average of its points and then re-sorting the points to the nearest center. It needs no labels: nobody tells it which point belongs where.
Start learning No account needed
If you picture dropping k pins on a map and letting every house join the nearest pin, then moving each pin to the middle of its houses, you have the idea. If you want the statistics around a method like this, learn statistics on your own covers the basics.
1. The pieces
The scikit-learn documentation says the algorithm clusters data by trying to separate samples into groups of equal variance, minimizing a criterion called the inertia, or within-cluster sum of squares. It requires the number of clusters to be specified. Each of the k clusters is described by the mean of its samples, which the documentation calls the centroid. It adds that centroids are not, in general, points from the data, although they live in the same space.
Inertia is the sum, over all samples, of the squared distance from the sample to its nearest centroid. Low inertia means the points sit close to their centers. The documentation describes the procedure, often called Lloyd’s algorithm, in three steps. First, choose initial centroids; the most basic method is to pick k of the samples. Then loop between two steps: assign each sample to its nearest centroid, and create new centroids by taking the mean of all the samples assigned to each one. It repeats until the centroids do not move significantly.
2. A worked example: six students, two groups
Six students reported how many hours a week they study: 1, 2, 5, 7, 11 and 12. The numbers are made up for this page. We want two groups, so k is 2. We start with two of the students’ values as centroids, 1 and 2.
Round 1: with centroids 1 and 2, the student at 1 is nearest to the first centroid, and the other five are nearest to the second, so the groups are {1} and {2, 5, 7, 11, 12}. The inertia is 215. The new centroids are the group means, 1 and 7.4. Round 2: with centroids 1 and 7.4, the groups are {1, 2} and {5, 7, 11, 12}, the inertia is 41.04, and the new centroids are 1.5 and 8.75. Round 3: the groups are {1, 2, 5} and {7, 11, 12}, the inertia is 31.438, and the new centroids are 2.667 and 10. Round 4: the groups do not change, the centroids stay at 2.667 and 10, and the inertia is 22.667. We stop, because the centroids did not move. The inertia fell in every round until the centroids stopped moving. The first group’s center, 2.667 hours, is not one of the six students.
The six values in their two final groups, with the centroids as triangles.
3. Why the starting points can change the answer
The documentation says that, given enough time, k-means will converge, but this may be to a local minimum, and the result depends heavily on how the centroids are initialized. For that reason it says the computation is often done several times with different initializations. Our example shows the issue. Start again, this time with the students at 7 and 11 as centroids. Round 1 gives the groups {1, 2, 5, 7} and {11, 12}, with inertia 66, and new centroids 3.75 and 11.5. Round 2 gives the same groups with inertia 23.25, and the centroids do not move. The method stopped, but 23.25 is higher than the 22.667 we reached from the first start. We split all six values into two groups every possible way in the script, and 22.667 is the lowest inertia any split reaches.
How common is this? There are 15 ways to pick two of the six values as starting centroids. In the script, 9 of them end at 22.667 and 6 end at 23.25 (a tie between centroids goes to the first one). The documentation says one way to address the problem is k-means++ initialization, which starts the centroids generally distant from each other and leads to probably better results than random initialization. We did not run k-means++ here.
4. A common misconception: k-means finds the groups that are in the data
It is natural to read the output as a discovery. But the method returns k groups for whatever k you give it. In our example the lowest inertia is 103.333 with one group, 22.667 with two, 3 with three, 1 with four, 0.5 with five and 0 with six, where every student is alone. Inertia fell at each step, so it cannot tell you by itself which k to use. The documentation also lists limits of the criterion: it assumes clusters are convex and isotropic, which does not hold for every dataset, and it responds poorly to elongated clusters or manifolds with irregular shapes. It also says inertia is not a normalized metric, so you only know that lower values are better. Treat the groups as a summary to check against what you know about the data, not as a fact about it.
5. Test yourself
Close this page and take four values: 2, 4, 10 and 12, with k = 2 and starting centroids 2 and 12. Work out the two groups in the first round, the new centroids, and the inertia once those centroids are in place. Then say what the inertia would be for a single group. Reopen the page and compare. The groups are {2, 4} and {10, 12}, the new centroids are 3 and 11, and the inertia is 1 + 1 + 1 + 1 = 4. For one group the center is 7 and the inertia is 25 + 9 + 9 + 25 = 68. The first step where you slipped is the part to study again. For the general method, see how to test yourself.
What the sources found, and their limits
scikit-learn user guide, section 2.3.2 K-means. Documentation for the scikit-learn library. It describes k-means as separating samples into groups of equal variance by minimizing inertia, with the number of clusters specified in advance and each cluster described by the mean of its samples. It lays out the three steps (initial centroids, assign to the nearest centroid, recompute the means), says the loop repeats until the centroids do not move significantly, warns that the result can be a local minimum that depends on initialization, describes k-means++ initialization, and lists the convex and isotropic assumption, the poor fit for elongated clusters and the fact that inertia is not normalized. Source: scikit-learn.org, clustering, k-means, read on 2026-11-23.
Limits: the source is software documentation, not a textbook, so we read it for what the method does and not for proofs. We did not read its mini-batch variant, its parallelism notes, or the linked examples on assumptions and silhouette analysis beyond their titles. The six students, the two starts, the round-by-round values, the counts over starting pairs and the test-yourself values are our own arithmetic in the script and are not from the documentation. The data are made up.
How Professor Turing helps
Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start.
Bring a topic you are studying, such as why two different starting points can end in different groups. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, such as a term, it asks how you got there instead of saying right or wrong, and when you stay stuck it changes its approach and makes the next step smaller instead of giving the final answer. Later in a session it can offer a short test without help, scored apart from the tutoring, so you can tell what you can do alone from what the tutor carried you through.
It does not read or mark a written answer, and it does not write your explanation for you, so the explanation has to be yours. You must be 18 or older, and conversations are deleted after 7 days.
FAQ
Is a cluster center one of my data points? No, not in general. The scikit-learn documentation describes each center as the mean of the samples in its cluster and notes that these means are not, in general, points from the data, although they live in the same space. In our example the center of the first group is 2.667 hours, and no student studied that long.
Why not choose the number of clusters with the lowest inertia? Because inertia falls each time you add a cluster. In our example it is 103.333 for one cluster, 22.667 for two, 3 for three and 0 for six, where every student is alone in a cluster, which tells you nothing about groups. The documentation says the number of clusters has to be specified, and it points to a silhouette analysis example for selecting it, which we did not read.
Start with what you're working on
Tell the tutor which topic you are studying and let it ask you the questions.
Start learning No account needed
See also: all study guides