Back to Classic ML Mind Map
中文·English
📊 Classic MLID: kmeans

K-Means Algorithm

K-Means 算法
🎯Core Definition
K-Means partitions nn samples into KK mutually exclusive hard clusters by coordinate descent on the within-cluster sum of squares (WCSS) objective J=k=1KxCkxμk2J = \sum_{k=1}^{K} \sum_{x \in C_k} \Vert x - \mu_k \Vert^2.Each iteration alternates two steps: ① assignment — fix centroids, assign each xx to the nearest one, c(x)=argminkxμkc(x) = \arg\min_k \Vert x - \mu_k \Vert; ② update — fix assignments, set each centroid to the cluster mean μk=1CkxCkx\mu_k = \tfrac{1}{|C_k|} \sum_{x \in C_k} x.Both steps never increase JJ, so the algorithm always converges — but only to a local optimum; each round costs O(nKd)O(nKd), total O(nKdT)O(nKdT) with TT iterations.
💡Use Cases
exploratory clustering of unlabeled data (user/item segmentation, personas), image color quantization, vector quantization, and as the hard-assignment special case of GMM; interviews ask for the objective, convergence, initialization, and choosing KK.
Key Problems Solved
unlike hierarchical clustering with its O(n2)O(n^2) similarity matrix, K-Means is linear-time and scales to millions of samples; unlike GMM it needs no covariance estimation — simple, fast, and easy to implement. The tradeoff: convex (spherical) hard clusters only — poor on non-convex or uneven-density data, and the result is strongly initialization-dependent (see K-Means++).
🎯5 High-Frequency Exam Points
1
Write the K-Means objective J=kxCkxμk2J = \sum_{k} \sum_{x \in C_k} \Vert x - \mu_k \Vert^2; why do the assignment and update steps never increase JJ, guaranteeing convergence?
2
Derive the centroid update μk=1CkxCkx\mu_k = \tfrac{1}{|C_k|} \sum_{x \in C_k} x as the minimizer of the subproblem; is K-Means a convex problem?
3
K-Means vs GMM (hard vs soft assignment)? How is K-Means fundamentally different from KNN?
4
Why does K-Means only reach a local optimum? How does initialization affect the result? Mitigations: K-Means++, multiple random restarts, K-Medoids?
5
What is the time complexity of K-Means? How to pick K (elbow method, silhouette)? Why is it outlier-sensitive and why does it fail on non-convex clusters?
📖 In-depth Guide:📄 clustering-and-knn
Updated 2026-08-12
🎯
Test Your Knowledge: Practice Questions for "K-Means Algorithm"
Single choice pitfall questions with instant feedback and mistake tracking.
🚀 Start Card Practice
Previous CardEnsemble ComparisonNext CardK-Means++ & K Selection

🔗 More Classic ML Knowledge Cards

AdaBoost DerivationBagging & Random ForestBaum-Welch (HMM EM)GBDT Negative Gradient