← Back to Classic ML Mind Map
📊 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=1K∑x∈Ck∥x−μk∥2J = \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)=arg⁡min⁡k∥x−μk∥c(x) = \arg\min_k \Vert x - \mu_k \Vert; ② update — fix assignments, set each centroid to the cluster mean μk=1∣Ck∣∑x∈Ckx\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++).
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of K-Means Algorithm.
Use Cases
In production engineering, what are the primary use cases for K-Means Algorithm and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Write the K-Means objective J=∑k∑x∈Ck∥x−μk∥2J = \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=1∣Ck∣∑x∈Ckx\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
🎯
Verify Mastery: Practice Exam Questions for "K-Means Algorithm"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardEnsemble ComparisonNext Card →K-Means++ & K Selection

🔗 More Classic ML Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/ML/card/kmeans
中文速览与面试要点

K-Means 算法 (K-Means Algorithm)

定义: K-Means (K 均值) 是将 n 个样本划分为 K 个互斥硬簇的经典聚类算法, 通过坐标下降最小化簇内平方和 (WCSS) 目标 J = _k=1^K _x C_k x - _k ^2。每轮交替两步: ① 分配步 — 固定质心, 每个样本 x 指派给最近质心 c(x) = _k x - _k ; ② 更新步 — 固定分配, 质心取簇内均值 _k = 1|C_k| _x C_k x。两步都只减不增 J, 故必收敛, 但只能收敛到局部最优; 每轮复杂度 O(nKd), 总复杂度 O(nKdT) (T 为迭代轮数)。 使用场景: 无标签数据的探索性分群 (用户/商品聚类、画像)、图像颜色量化、向量量化、作为 GMM 硬分配的特例对照; 面试常考目标函数手推、收敛性、初始化与 K 的选择。 解决的问题: 相比层次聚类需 O(n^2) 的相似度矩阵内存, K-Means 线性时间可扩展至百万级样本; 相比 GMM 无需估计协方差, 简单、快、易实现。代价是只能产出凸形 (球形) 硬簇, 对非凸簇与密度不均数据效果差, 且结果强依赖初始化 (见 K-Means++)。

核心要点

  • 手推 K-Means 目标函数 J = _k _x C_k x - _k ^2? 为什么分配步与更新步能保证 J 单调不增、算法必收敛?
  • 推导质心更新公式 _k = 1|C_k| _x C_k x: 固定分配后对 _k 求导置零得最优解; K-Means 是凸优化吗?
  • K-Means 与 GMM 的区别 (硬分配 vs 软分配)? K-Means 与 KNN 有什么本质不同?
  • K-Means 为什么只能收敛到局部最优? 初始化如何影响结果? 缓解手段有哪些 (K-Means++、多次随机重启、K-Medoids)?
  • K-Means 的时间复杂度是多少? 如何选 K (肘部法、轮廓系数)? 为什么对异常值敏感、对非凸簇失效?