k-means clustering
In statistics and machine learning, k-means clustering is a method of cluster analysis which aims to partition n observations into k clusters in which each observation belongs to the cluster with the nearest mean. It is similar to the expectation-maximization algorithm for mixtures of Gaussians in that they both attempt to find the centers of natural clusters in the data as well as in the iterative refinement approach employed by both algorithms.
Description
Given a set of observations (x1, x2, …, xn), where each observation is a d-dimensional real vector, k-means clustering aims to partition the n observations into k sets (k ≤ n) S = {S1, S2, …, Sk} so as to minimize the within-cluster sum of squares (WCSS):
where μi is the mean of points in Si.
History
The term "k-means" was first used by James MacQueen in 1967,[1] though the idea goes back to Hugo Steinhaus in 1956.[2] The standard algorithm was first proposed by Stuart Lloyd in 1957 as a technique for pulse-code modulation, though it wasn't published until 1982.[3]
Algorithms
Regarding computational complexity, the k-means clustering problem is:
- NP-hard in general Euclidean space d even for 2 clusters [4][5]
- NP-hard for a general number of clusters k even in the plane [6]
- If k and d are fixed, the problem can be exactly solved in time O(ndk+1 log n), where n is the number of entities to be clustered [7]
Thus, a variety of heuristic algorithms are generally used.
Standard algorithm
The most common algorithm uses an iterative refinement technique. Due to its ubiquity it is often called the k-means algorithm; it is also referred to as Lloyd's algorithm, particularly in the computer science community.
Given an initial set of k means m1(1),…,mk(1), which may be specified randomly or by some heuristic, the algorithm proceeds by alternating between two steps:[8]
- Assignment step: Assign each observation to the cluster with the closest mean (i.e. partition the observations according to the Voronoi diagram generated by the means).
- Update step: Calculate the new means to be the centroid of the observations in the cluster.
The algorithm is deemed to have converged when the assignments no longer change.
- Demonstration of the standard algorithm
-
1) k initial "means" (in this case k=3) are randomly selected from the data set (shown in color).
-
2) k clusters are created by associating every observation with the nearest mean. The partitions here represent the Voronoi diagram generated by the means.
-
3) The centroid of each of the k clusters becomes the new means.
-
4) Steps 2 and 3 are repeated until convergence has been reached.
As it is a heuristic algorithm, there is no guarantee that it will converge to the global optimum, and the result may depend on the initial clusters. As the algorithm is usually very fast, it is common to run it multiple times with different starting conditions. However, in the worst case, k-means can be very slow to converge: in particular it has been shown that there exist certain point sets, even in 2 dimensions, on which k-means takes exponential time, that is 2Ω(n), to converge[9][10]. These point sets do not seem to arise in practice: this is corroborated by the fact that the smoothed running time of k-means is polynomial[11].
The "assignment" step is also referred to as expectation step, the "update step" as maximization step, making this algorithm a variant of the generalized expectation-maximization algorithm.
Variations
- The expectation-maximization algorithm (EM algorithm) maintains probabilistic assignments to clusters, instead of deterministic assignments, and multivariate Gaussian distributions instead of means.
- k-means++ seeks to choose better starting clusters.
- The filtering algorithm uses kd-trees to speed up each k-means step.[12]
- Some methods attempt to speed up each k-means step using coresets[13] or the triangle inequality.[14]
- Escape local optima by swapping points between clusters.[15]
Discussion


The two key features of k-means which make it efficient are often regarded as its biggest drawbacks:
- Euclidean distance is used as a metric and variance is used as a measure of cluster scatter.
- The number of clusters k is an input parameter: an inappropriate choice of k may yield poor results. That is why, when performing k-means, it is important to run diagnostic checks for determining the number of clusters in the data set.
A key limitation of k-means is its cluster model. The concept is based on spherical clusters that are separable in a way so that the mean value converges towards the cluster center. The clusters are expected to be of similar size, so that the assignment to the nearest cluster center is the correct assignment. When for example applying k-means with a value of onto the well-known Iris flower data set, the result often fails to separate the three Iris species contained in the data set. With , the two visible clusters (one containing two species) will be discovered, whereas with one of the two clusters will be split into two even parts. In fact, is more appropriate for this data set, despite the data set containing 3 classes. As with any other clustering algorithm, the k-means result relies on the data set to satisfy the assumptions made by the clustering algorithms. It works very well on some data sets, while failing miserably on others.
The result of k-means can also be seen as the Voronoi cells of the cluster means. Since data is split halfway between cluster means, this can lead to suboptimal splits as can be seen in the "mouse" example. The Gaussian models used by the Expectation-maximization algorithm (which can be seen as a generalization of k-means) are more flexible here by having both variances and covariances. The EM result is thus able to accommodate clusters of variable size much better than k-means as well as correlated clusters (not in this example).
Applications of the algorithm
Image segmentation
The k-means clustering algorithm is commonly used in computer vision as a form of image segmentation. The results of the segmentation are used to aid border detection and object recognition. In this context, the standard Euclidean distance is usually insufficient in forming the clusters. Instead, a weighted distance measure utilizing pixel coordinates, RGB pixel color and/or intensity, and image texture is commonly used.[16]
Relation to Principal Component Analysis
It has been shown[17][18] that the relaxed solution of k-means clustering, specified by the cluster indicators, is given by the PCA (principal component analysis) principal components, and the PCA subspace spanned by the principal directions is identical to the cluster centroid subspace specified by the between-class scatter matrix.
Similar problems
The set of squared error minimizing cluster functions also includes the k-medoids algorithm, an approach which forces the center point of each cluster to be one of the actual points, i.e., it uses medoids in place of centroids.
Software
Free
- R kmeans implements a variety of algorithms[1][3][15]
- Apache Mahout k-Means
- SciPy vector-quantization
- Silverlight widget demonstrating k-means algorithm
Commercial
See also
- Cluster analysis
- Data Mining
- Linde–Buzo–Gray algorithm
- k-medians clustering
- k-medoid
- Silhouette
- k-means++
- Centroidal Voronoi tessellation
- Canopy clustering algorithm
- Determining the number of clusters in a data set
- Data Mining in Agriculture
Notes
- ^ a b MacQueen, J. B. (1967). Some Methods for classification and Analysis of Multivariate Observations. Proceedings of 5th Berkeley Symposium on Mathematical Statistics and Probability. Vol. 1. University of California Press. pp. 281–297. MR0214227. Zbl 0214.46201. Retrieved 2009-04-07.
- ^ Steinhaus, H. (1956). "Sur la division des corps matériels en parties". Bull. Acad. Polon. Sci. (in French). 4 (12): 801–804. MR0090073. Zbl 0079.16403.
- ^ a b Lloyd, S. P. (1957). "Least square quantization in PCM". Bell Telephone Laboratories Paper. Published in journal much later: Lloyd., S. P. (1982). "Least squares quantization in PCM" (PDF). IEEE Transactions on Information Theory. 28 (2): 129–137. doi:10.1109/TIT.1982.1056489. Retrieved 2009-04-15.
- ^ Aloise, D.; Deshpande, A.; Hansen, P.; Popat, P. (2009). "NP-hardness of Euclidean sum-of-squares clustering". Machine Learning. 75: 245–249. doi:10.1007/s10994-009-5103-0.
{{cite journal}}
: CS1 maint: multiple names: authors list (link) - ^
Dasgupta, S. and Freund, Y. (2009). "Random Projection Trees for Vector Quantization". Information Theory, IEEE Transactions on. 55: 3229–3242. doi:10.1109/TIT.2009.2021326.
{{cite journal}}
: Unknown parameter|month=
ignored (help)CS1 maint: multiple names: authors list (link) - ^ Mahajan, M.; Nimbhorkar, P.; Varadarajan, K. (2009). "The Planar k-Means Problem is NP-Hard". Lecture Notes in Computer Science. 5431: 274–285. doi:10.1007/978-3-642-00202-1_24.
{{cite journal}}
: CS1 maint: multiple names: authors list (link) - ^
Inaba, M.; Katoh, N.; Imai, H. (1994). Applications of weighted Voronoi diagrams and randomization to variance-based k-clustering. Proceedings of 10th ACM Symposium on Computational Geometry. pp. 332–339. doi:10.1145/177424.178042.
{{cite conference}}
: CS1 maint: multiple names: authors list (link) - ^ Mackay (2003)
- ^ Vattani, A. (2009). "k-means requires exponentially many iterations even in the plane" (PDF). Proceedings of the 25th Symposium on Computational Geometry (SoCG).
{{cite conference}}
: Unknown parameter|booktitle=
ignored (|book-title=
suggested) (help) - ^ Arthur, D.; Vassilvitskii, S. (2006). "How Slow is the k-means Method?" (PDF). Proceedings of the 22nd Symposium on Computational Geometry (SoCG).
{{cite conference}}
: Unknown parameter|booktitle=
ignored (|book-title=
suggested) (help)CS1 maint: multiple names: authors list (link) - ^ Arthur, D.; Manthey, B.; Roeglin, H. (2009). "k-means has polynomial smoothed complexity". Proceedings of the 50th Symposium on Foundations of Computer Science (FOCS).
{{cite conference}}
: Unknown parameter|booktitle=
ignored (|book-title=
suggested) (help)CS1 maint: multiple names: authors list (link) - ^ Kanungo, T.; Mount, D. M.; Netanyahu, N. S.; Piatko, C. D.; Silverman, R.; Wu, A. Y. (2002). "An efficient k-means clustering algorithm: Analysis and implementation" (PDF). IEEE Trans. Pattern Analysis and Machine Intelligence. 24: 881–892. doi:10.1109/TPAMI.2002.1017616. Retrieved 2009-04-24.
{{cite journal}}
: CS1 maint: multiple names: authors list (link) - ^ Frahling, G.; Sohler, C. (2006). "A fast k-means implementation using coresets" (PDF). Proceedings of the twenty-second annual symposium on Computational geometry (SoCG).
{{cite conference}}
: Unknown parameter|booktitle=
ignored (|book-title=
suggested) (help)CS1 maint: multiple names: authors list (link) - ^ Elkan, C. (2003). "Using the triangle inequality to accelerate k-means" (PDF). Proceedings of the Twentieth International Conference on Machine Learning (ICML).
{{cite conference}}
: Unknown parameter|booktitle=
ignored (|book-title=
suggested) (help) - ^ a b Hartigan, J. A.; Wong, M. A. (1979). "Algorithm AS 136: A K-Means Clustering Algorithm". Journal of the Royal Statistical Society, Series C (Applied Statistics). 28 (1): 100–108. JSTOR 2346830.
- ^ Shapiro, Linda G.; Stockman, George C. (2001). Computer Vision. Upper Saddle River, NJ: Prentice Hall. ISBN 0130307963.
- ^ H. Zha, C. Ding, M. Gu, X. He and H.D. Simon. "Spectral Relaxation for K-means Clustering", Neural Information Processing Systems vol.14 (NIPS 2001). pp. 1057-1064, Vancouver, Canada. Dec. 2001.
- ^ Chris Ding and Xiaofeng He. "K-means Clustering via Principal Component Analysis". Proc. of Int'l Conf. Machine Learning (ICML 2004), pp 225-232. July 2004.
References and further reading
- Hartigan, J. A. (1975). Clustering Algorithms. Wiley. ISBN 0-471-35645-X. MR0405726.
- MacKay, David (2003). "Chapter 20. An Example Inference Task: Clustering". Information Theory, Inference and Learning Algorithms. Cambridge University Press. pp. 284–292. ISBN 0-521-64298-1. MR2012999.
{{cite book}}
: External link in
(help); Unknown parameter|chapterurl=
|chapterurl=
ignored (|chapter-url=
suggested) (help) - Świniarski, Roman; Cios, Krzysztof J.; Pedrycz, Witold (1998). Data mining methods for knowledge discovery. Kluwer Academic. ISBN 0-7923-8252-8.
{{cite book}}
: CS1 maint: multiple names: authors list (link)
External links
![]() | This article's use of external links may not follow Wikipedia's policies or guidelines. (August 2010) |
Code implementations
- K-means application in PHP
- K-means clustering using VB
- A fast implementation of the K-means algorithm which uses the triangle inequality to speed up computation
- K-means clustering using Perl. Online clustering.
- K-means clustering using C++ by Antonio Gulli
- K-means clustering using Matlab
- K-means clustering implementation in Ruby (AI4R)
- Another K-means clustering implementation in Ruby
- k-means clustering implementation in Python with scipy
- k-means in X10
- A parallel out-of-core implementation in C
- An open-source collection of clustering algorithms, including k-means, implemented in Javascript (Online demo)
Visualization, animation and examples
- An example of multithreaded application which uses K-means in Java
- Visualization of the K-means-algorithm (Applet)
- Interactive demo of the K-means-algorithm (Applet)
- Another animation of the K-means-algorithm
- Numerical Example of K-means clustering
- Application example which uses K-means clustering to reduce the number of colors in images
- Clustergram - cluster diagnostic plot - for visual diagnostics of choosing the number of (k) clusters (R code)