
k-means
k-means is a method that automatically sorts data points into a predefined number of groups – without anyone specifying in advance which point belongs to which group. It is one of the most widely used methods in machine learning because it is fast and easy to understand.
k-means is a method that automatically sorts data points into groups. The number of groups is set in advance – that’s the k. Within each group, the points should be as similar as possible. Between groups, the differences should be as large as possible. The method doesn’t need ready-made examples with labels; it discovers the structure on its own. This is called unsupervised learning.
Why k-means is so widely used
Many datasets come without ready-made categories. An online shop has millions of customers, but nobody has manually sorted them into “bargain hunters” and “brand buyers”. k-means does exactly that automatically. It finds patterns that were previously invisible.
The second reason for its popularity is speed. Even on large datasets with hundreds of thousands of points, k-means delivers a result in seconds. More complex methods take hours for the same task. For a first overview of unknown data, k-means is therefore often the first tool reached for.
A common misconception: k-means finds the “correct” groups. It only finds the computationally best groups for the chosen k. Whether three or seven groups make more sense is for a human to decide. The method itself has no opinion on this.
How the algorithm works
At the start, the algorithm selects k random points as centers, so-called centroids. Then each data point is assigned to the nearest centroid. This produces k preliminary groups.
In the second step, the algorithm calculates new centroids: it takes the geometric center of all points in a group. Then the assignment starts over – this time to the new centroids. These two steps repeat until the assignments no longer change. This usually takes only a few iterations.
A practical problem: the random start can cause the algorithm to get stuck with a poor result. That’s why it’s run multiple times with different random starting points, and the best solution is chosen in the end. The k-means++ variant selects the starting points more deliberately and largely avoids this problem.
k-means in products and headlines
Streaming services like Spotify or Netflix use similar methods to sort users into taste groups. Whoever ends up in the same group as many others gets recommended their favorite shows. k-means is often the first step before more elaborate methods take over.
In medicine, k-means helps find groups of patients with similar symptoms. This allows researchers to discover whether a disease is actually made up of several different variants. In image processing, k-means compresses colors: a photo with millions of shades can be reduced to 16 representative colors without looking much worse.
In financial news, the term comes up when banks explain how they check transactions for suspicious patterns. Unusual payments stand out because they don’t fit into any of the normal groups. k-means helps define what’s normal – and thereby makes the abnormal visible.