What is k-means clustering?
K-means is an unsupervised clustering algorithm that splits data into k groups, assigning each point to the cluster with the nearest center, called a centroid. It alternates two steps, assigning points to their nearest centroid and moving each centroid to the mean of its points, until the assignments stop changing. The result minimizes, at least locally, the total squared distance from points to their centroids.
K-means is used for customer segmentation, image color quantization, grouping documents, and compressing embedding vectors, as in the vector quantization inside approximate nearest-neighbor indexes. It is fast and simple, but it assumes roughly round clusters of similar size, and k has to be chosen in advance.
Key ideas
- K-means splits data into k clusters, each represented by the mean of its points.
- It alternates an assignment step and an update step until the assignments stop changing.
- Both steps lower the inertia, the total squared distance from points to their centroids.
- It always converges, but often to a local optimum that depends on the starting centroids.
- k-means++ initialization and several random restarts make a good result much more likely.
- The elbow of the inertia curve is a common heuristic for choosing k.
Short takes on this topic
Common questions
What is the difference between k-means and k-NN?
K-means is unsupervised clustering, and its k is the number of clusters it finds. k-nearest neighbors is supervised classification or regression, and its k is the number of labeled neighbors that vote. Both depend on distances, which is the main source of the confusion.
When does k-means fail?
It struggles when clusters are elongated, curved, nested, or very different in size or density, because it draws straight boundaries halfway between centroids. Outliers also drag centroids away from the bulk of a cluster. DBSCAN handles arbitrary shapes and noise, and Gaussian mixture models handle elliptical clusters of different sizes.
How is k-means related to Gaussian mixture models?
A Gaussian mixture fitted with expectation-maximization gives each point a soft probability of belonging to each cluster. If every component has the same small spherical variance, those soft assignments become hard nearest-centroid assignments, and the algorithm reduces to k-means.
Does k-means need feature scaling?
Usually. Squared Euclidean distance adds up differences across features, so a feature measured in large units dominates the clustering. Standardizing each feature first gives all of them a comparable influence, unless the original units are meaningful and comparable.
Related explainers
- K-Nearest Neighbors — Classification by proximity and distance metrics
- Dimensionality Reduction — PCA and t-SNE — seeing high-dimensional data
- Norms & Distance Metrics — L1, L2, cosine similarity — measuring closeness in vector spaces
- Feature Scaling — Why and how to normalize features for faster convergence
- Curse of Dimensionality — Why high-dimensional spaces break intuition and algorithms
- Embeddings — Turning words and categories into vectors
References
- Lloyd (1982), "Least Squares Quantization in PCM", IEEE Transactions on Information Theory
- James, Witten, Hastie & Tibshirani, "An Introduction to Statistical Learning", chapter 12: Unsupervised Learning
Last updated