K-means clustering is an unsupervised algorithm that partitions data into k clusters by repeatedly assigning points to the nearest centroid and recomputing those centroids until they stop moving.
K-means clustering divides a dataset into k groups by minimizing the squared distance between each point and its assigned cluster center. The algorithm alternates between assigning points to the nearest centroid and updating each centroid to the mean of its assigned points.
That alternating loop is simple, fast, and surprisingly effective for well-separated spherical clusters. The catch is that k-means makes strong assumptions: clusters are roughly equal-sized, convex, and isotropic. Real data often violates those assumptions, which is why silhouette scoring and elbow methods matter before trusting the result.
Think of it like this. Think of a delivery manager placing k warehouses to minimize total travel distance. They move each warehouse to the center of the houses it serves, then reassign houses to the nearest warehouse, and repeat until everything stabilizes.
The algorithm initializes k centroids, assigns each point to the nearest centroid by Euclidean distance, recomputes each centroid as the mean of its assigned points, and repeats until assignments stop changing. Initialization methods include random selection and k-means++, which spreads initial centroids to improve convergence.
"k-means works for any clustering task." It fails on non-convex, density-based, or categorical data. "The elbow method always gives the answer." It is a heuristic; domain knowledge often decides k more reliably. "K must be large to capture patterns." Excess clusters fragment meaningful groups.
Fast and scalable to millions of points, but requires specifying k in advance and assumes spherical equally sized clusters. Great for market segmentation and image quantization; weak for arbitrary shapes without preprocessing.