K-means clustering

K-means clustering is an unsupervised learning method that splits data into a chosen number of groups, k, by repeatedly assigning each point to the nearest group centre and moving each centre to the average of its points.

K-means is one of the oldest and most widely used ways to find groups in data that has no labels. You choose how many groups you want, k, and place k starting centres, called centroids. Then two steps repeat: assign each data point to its nearest centroid, then move each centroid to the average position of the points assigned to it. When the assignments stop changing, the groups are done.

Google's machine learning glossary gives a practical example. A manufacturer of dog sweaters measures the height and width of many dogs and runs k-means with k set to 3. The three centroids give the average height and width of the dogs in each group, which suggests sizes for small, medium and large sweaters. The same method works with many measurements at once, not just two.

The algorithm tries to minimise the total squared distance from each point to its centroid, a quantity scikit-learn calls inertia. It always settles, but not necessarily on the best answer: finding the true optimum is NP-hard, and the standard algorithm can stop at a local optimum that depends on where the centroids start. In practice the algorithm is run several times from different starting points, and a method called k-means++ picks starting centroids spread far apart, which usually gives better results than random placement.

The idea goes back to Hugo Steinhaus in 1956. Stuart Lloyd of Bell Labs proposed the standard algorithm in 1957 for pulse-code modulation, although it was not published as a journal article until 1982, and Edward Forgy published essentially the same method in 1965. James MacQueen coined the name k-means in 1967.

K-means has clear limits. You must choose k in advance. It assumes groups that are roughly round and similar in size, so it handles elongated or irregular clusters badly. Distances become inflated in very high dimensions, which is why scikit-learn suggests reducing dimensions with PCA first. It is used for customer segmentation, for reducing an image to a small palette of colours and as a step inside other methods. The groups it finds are a summary of the data, not a discovery of natural categories; a person still has to decide what, if anything, they mean.

Common questions

How does k-means clustering work?

You choose a number of clusters, k, and k starting centres. Each point is assigned to its nearest centre, each centre moves to the average of its points, and the two steps repeat until the assignments stop changing. The result is k groups of points that are close to their centres.

How do you choose k in k-means?

There is no formula that gives the right k. Analysts usually try several values and compare how tight and how distinct the resulting clusters are, or choose a number that suits the purpose, such as three sweater sizes.

Is k-means supervised or unsupervised?

Unsupervised. It works on data without labels and finds groups based only on how close points are to each other. Any meaning given to the groups is added afterwards by a person.

What is the difference between k-means and k-nearest neighbours?

They solve different problems. K-means is an unsupervised clustering method that groups unlabelled data into k clusters. K-nearest neighbours is a supervised method that predicts the label of a new point from the labels of the k closest points in a labelled dataset.

Finding more

Search for "k-means clustering", "k-means++" or "choosing the number of clusters". The scikit-learn clustering guide and Google's clustering course explain the method with examples, and statistics textbooks cover its theory. Be cautious about stories that treat clusters found by k-means as natural categories, since the method will find k groups whether or not they exist.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.