K-Means Clustering
K-Means Clustering — K-means is an unsupervised clustering algorithm that partitions data into k groups by alternating between assigning each point to its nearest centroid and moving each centroid to the mean of its members, minimizing within-cluster variance.
K-means finds groups in your data by repeating two simple steps: assign each point to the nearest center, then move each center to the middle of its group. Repeat until nothing changes.
- Cluster 1
- Cluster 2
- Cluster 3
- Centroid
Clustering controls
The idea in plain words
K-means finds groups by repeating two steps until nothing changes: assign each point to its nearest center, then move each center to the average of its points. Step through the iterations and watch the centers slide into place.
Because the objective isn’t convex, a bad starting placement can converge to an obviously wrong grouping — crowd the centers in one corner to see it. Unlike k-nearest neighbors, there are no labels; k-means discovers structure on its own.
Now, the math
It minimizes the within-cluster sum of squares (inertia):
- a data point.
- the centroid of the cluster point i is assigned to.
- total inertia — smaller means tighter clusters.
▸ Show the derivation
The assign and update steps each never increase J, so the algorithm always converges — but only to a local minimum. In practice you run it several times from different starts (or use k-means++) and keep the lowest-inertia result.
Trace it by hand
Five points — (1,1), (2,1), (1,2), (6,5), (7,6) — and k = 2 centroids starting at mu1 = (0,0) and mu2 = (4,4). One assign step and one update step, with squared Euclidean distances throughout (fractions kept exact; decimals rounded to 2 places).
Step 1 — assign each point to its nearest centroid
Each point simply picks the smaller squared distance: three points join cluster 1, two join cluster 2.
Step 2 — total the winning distances: inertia
J sums each point's squared distance to its own centroid. The starting placement scores 30.
Step 3 — move each centroid to the mean of its points
The update step is just an average per cluster — no distances involved.
Step 4 — re-assign and watch the inertia fall
No point changes cluster, so the algorithm has converged — inertia dropped from 30 to 2.33 in a single round.
What just happened: One assign-update round cut the inertia from 30 to 2.33 and nothing moved afterwards: each step provably never increases J, which is why k-means always settles — though only into a local minimum.
Now Break It
Try this: Bad initial centroid placement gets stuck in a terrible local minimum — clusters are obviously wrong.
Control: Drag centroids to adversarial starting positions (e.g., all in one corner)
What happens: Stuck in a local minimum! The algorithm converged, but the clusters are clearly wrong. Initialization matters.
Where k-means clustering is used
K-Means clustering is a workhorse for turning raw records into actionable groups. Retailers use it for customer segmentation, splitting shoppers by spending, frequency, and recency so marketing can target each group differently. It compresses images by reducing millions of pixel colors to a small palette of representative centroids, and it powers document grouping when text is first turned into numeric vectors. Engineers use it as a fast preprocessing step to summarize sensor readings or to initialize more complex models. Because it scales to large datasets and runs quickly, K-Means is often the first clustering method people reach for when they need a rough but useful partition of unlabeled data into a chosen number of groups.
A common misconception is that K-Means discovers the correct number of clusters on its own. It does not; you must supply k in advance, and different values produce entirely different partitions, so techniques like the elbow method or silhouette scores are needed to choose it sensibly. Another pitfall is assuming the algorithm handles any cluster shape. K-Means minimizes squared distance to centroids, which biases it toward roughly spherical, similarly sized groups, so it struggles with elongated or nested shapes. Results also depend on initialization and feature scaling: unscaled features let large-magnitude columns dominate, and a single run can settle into a poor local optimum, which is why running k-means++ initialization several times is standard practice.
Frequently asked questions
How do I choose the number of clusters k?
Why do I get different results each time I run K-Means?
Do I need to scale my features before clustering?
Can K-Means handle categorical data?
What is the difference between K-Means and KNN?
Written & reviewed by the ML Visualization team · Last updated .