K-means Clustering
Last Updated: July 29, 2026 | By Mihail Sebastian | AI Dictionary
An unsupervised algorithm that partitions data into k groups by assigning each point to the nearest centroid, then recomputing centroids until they settle.
What is K-means Clustering?
K-means clustering is an unsupervised algorithm that partitions data into \(k\) groups by assigning each point to its nearest cluster center, then moving each center to the mean of its points, until assignments stop changing. It is the workhorse of clustering: finding structure in data that has no labels.
You choose \(k\) before running it. The algorithm finds the groups; deciding how many groups to look for stays your job.
How K-means Clustering Works
- Initialize: Place \(k\) centroids at randomly chosen data points to start.
- Assign: Attach every point to its nearest centroid, measured by straight-line (Euclidean) distance.
- Update: Move each centroid to the average position of the points assigned to it.
- Repeat: Alternate assign and update until no point changes cluster.
Each pass shrinks the total squared distance between points and their centroids:
\[ \sum_{i=1}^{k} \sum_{x \in S_i} \lVert x - \mu_i \rVert^2 \]The outcome depends on where the centroids start, so practitioners run the algorithm several times and keep the best result. To pick \(k\) itself, they plot this objective for a range of values and look for the “elbow” where extra clusters stop paying off.
One name-collision warning: K-means has nothing to do with k-nearest neighbors. K-means groups unlabeled data into \(k\) clusters, while k-NN classifies a new point by polling its \(k\) closest labeled neighbors; the shared letter is a coincidence of notation.
Example of K-means Clustering
An online retailer wants to group 50,000 customers for targeted campaigns, using two features per customer: annual spend and orders per year. Marketing asks for three segments, so \(k = 3\).
The first pass scatters three centroids and assigns every customer to the nearest one. After a dozen assign-update rounds the centroids stop moving, and three clear groups emerge: frequent small buyers, rare big spenders, and a low-activity majority.
No one labeled a single customer. The structure came out of the distances alone, which is what makes K-means useful, and why the segments need a human sanity check before anyone acts on them.
Related AI terms: Clustering · K-Nearest Neighbors · Unsupervised Learning · Instance-based Learning · Anomaly Detection
Did you like the K-means Clustering gist?
Learn about 250+ need-to-know artificial intelligence terms in the AI Dictionary.
Mihail Sebastian — Writes about AI governance, regulation, and the technology behind them. Placeholder bio — replace with a real credential line. About