K-Means Clustering
Assign each point to the nearest centroid, then move each centroid to the mean of its cluster. Repeat until the clusters stabilize.
Points are colored by their nearest centroid. Each iteration moves every cross to the mean of its cluster. Inertia measures total squared distance; it decreases as the clusters tighten.
What you are seeing
Choose k initial centroids. Each point is assigned to the closest one.
Recompute each centroid as the mean of the points in its cluster.
Repeat assignment and update until centroids stop moving.
Why it matters
K-means is a standard baseline for customer segmentation, image compression, vector quantization, and exploratory data analysis.
Complexity & details
Time per iteration: O(n × k × d) for n points, k clusters, d dimensions. Space: O(n + k).
Frequently asked questions
Does K-means always find the global optimum?+
No. It converges to a local optimum that depends on initialization. Run it several times or use k-means++ for better starts.
How do I choose k?+
Use the elbow method or silhouette score, and combine it with domain knowledge about how many groups are meaningful.