K-Means Clustering
K-means partitions points into clusters, each represented by its centroid, minimizing within-cluster variance.
Definition
Algorithm: (1) initialize centroids, (2) assign each point to nearest centroid, (3) update centroids as the mean of assigned points, (4) repeat until convergence.
Converges to a local minimum — different initializations can yield different results.
Intuition
The centroid is the "representative point" of a cluster; new points are assigned to the cluster of their nearest centroid.
K-means assumes clusters are spherical and evenly sized — it struggles with elongated or unequally-sized clusters.
Worked example
With on customer data, you might discover clusters corresponding to "budget shoppers", "regulars", and "high spenders".
Compressing colors in an image: cluster pixel colors into palette values, then replace each pixel with its centroid.
The math
Within-cluster sum of squares: .
Elbow method: plot WCSS vs ; the "elbow" where adding more clusters gives diminishing returns suggests a good .
In practice
Used for market segmentation, image quantization, and as a preprocessing step before supervised learning.
For high-dimensional data, consider PCA first to reduce dimensionality and speed up clustering.
Go deeper
- InteractiveK-Means Clustering Playground
More in Data science
Assembled from the ReLU.chat curated knowledge base. These explanations are concise on purpose; check the sources for anything important.