# 劳埃德算法

劳埃德算法（Lloyd's algorithm）是用于k均值聚类的一种迭代方法：它反复把每个数据点指派给离它最近的聚类中心，再以指派给该中心的那些点的均值替换该中心，由此得到的聚类是这两步操作的一个不动点。它优化的是k均值目标函数，即每个点到其最近中心的距离平方和，\( \varphi = \sum_{x \in X} \min_{c \in C_k} \|x - c\|^2 \)。<sup>[1](https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf)</sup> 该方法通常被称为k均值算法，但k均值目标函数很难优化，该算法对它而言只是一种启发式方法。<sup>[2](http://www.cs.cmu.edu/~ninamf/papers/cluster-chapter.pdf)</sup> 它有两条并行的源流：同样的迭代最早在信号处理中为最小二乘量化而提出，后来才与聚类、以及与质心沃罗诺伊剖分（把空间划分为各自离某个中心点最近的区域）联系起来。<sup>[3](https://math.gmu.edu/~memelian/pubs/pdfs/DEJ_SIAM_lloyd.pdf)</sup>

| 要点 | 细节 |
|---|---|
| 目标函数 | \( \varphi = \sum_{x \in X} \min_{c \in C_k} \|x - c\|^2 \)，即到最近中心的距离平方 <sup>[1](https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf)</sup> |
| 迭代 | 把每个点指派给最近的中心，再把每个中心设为其所辖点的均值 <sup>[4](https://ar5iv.labs.arxiv.org/html/1602.08254)</sup> |
| 每次迭代代价 | \( O(nkd) \)：指派需 \( O(nkd) \)，重算中心需 \( O(nd) \) <sup>[2](http://www.cs.cmu.edu/~ninamf/papers/cluster-chapter.pdf)</sup> |
| 总代价 | 迭代 \( w \) 次为 \( O(wnkd) \)；\( w \) 可以关于 \( n \) 超线性，最坏情况下甚至是指数级 <sup>[5](https://cs.baylor.edu/~hamerly/papers/2014_pca_chapter_hamerly_drake.pdf)</sup> |
| 收敛性 | 代价从不增加；有限步终止，但一般只收敛到局部最优 <sup>[2](http://www.cs.cmu.edu/~ninamf/papers/cluster-chapter.pdf)</sup> |
| 问题难度 | 即使只有两个簇，精确k均值也是NP难的 <sup>[1](https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf)</sup> |
| 最坏情况迭代次数 | 在 \( d = \Omega(\sqrt{n}) \) 维中，迭代次数的下界为 \( 2^{\Omega(\sqrt{n})} \) <sup>[6](https://link.springer.com/article/10.1007/s00454-011-9340-1)</sup> |

## 工作原理

给定任意一组 \( k \) 个中心，每个中心的邻域就是那些以它为最近中心的数据点；用几何的话说，就是落在它的沃罗诺伊单元里的点。<sup>[7](https://doi.org/10.1109/tpami.2002.1017616)</sup> 指派步骤重新计算这一划分，更新步骤则把每个中心移到其单元内点的均值处。

每一步都不会增加k均值代价：均值是使其所在簇的1均值（距离平方）代价最小的点，因此把某个中心重新设为其簇的均值只会降低代价。<sup>[2](http://www.cs.cmu.edu/~ninamf/papers/cluster-chapter.pdf)</sup> 由于代价单调下降，而把 \( n \) 个点分成 \( k \) 个簇的划分方式只有有限多种，该方法必在有限时间内到达不动点，迭代次数以 \( n^{O(kd)} \) 为界，即 \( n \) 个点的沃罗诺伊划分的最大数目。<sup>[2](http://www.cs.cmu.edu/~ninamf/papers/cluster-chapter.pdf)</sup> 对于处于一般位置的点——即没有哪个数据点到两个中心的距离相等——按经典论述，该不动点是畸变的一个局部极小点，但不一定是全局极小；而且正如下文2025年的反例所示，它也未必在每一种局部最优的定义下都成立。<sup>[7](https://doi.org/10.1109/tpami.2002.1017616)</sup>

有一点提醒是新近才出现的：2025年的一篇论文用反例表明，K均值算法并不总是收敛到局部最优解，这推翻了它所称的自20世纪80年代以来被广泛接受的一种看法，并指出塞利姆与伊斯梅尔（Selim and Ismail，1984）只证明了算法在有限次迭代内收敛。<sup>[8](https://arxiv.org/html/2506.06990v2)</sup> 这与标准的局部极小论述 <sup>[7](https://doi.org/10.1109/tpami.2002.1017616)</sup> 相抵触，而这一分歧尚未解决。

最坏情况下的迭代次数是指数级的：阿瑟与瓦西里维茨基（Arthur and Vassilvitskii）把已知最好的下界从 \( \Omega(n) \) 提高到 \( 2^{\Omega(\sqrt{n})} \)，其构造是在 \( d = \Omega(\sqrt{n}) \) 维中 <sup>[6](https://link.springer.com/article/10.1007/s00454-011-9340-1)</sup>；瓦塔尼（Vattani）又把这一指数级最坏情况推广到了平面上。<sup>[9](https://drops.dagstuhl.de/storage/00lipics/lipics-vol057-esa2016/LIPIcs.ESA.2016.78/LIPIcs.ESA.2016.78.pdf)</sup> 与这些最坏情况相对，该方法在斯皮尔曼与滕（Spielman and Teng）意义下具有多项式平滑复杂度 <sup>[9](https://drops.dagstuhl.de/storage/00lipics/lipics-vol057-esa2016/LIPIcs.ESA.2016.78/LIPIcs.ESA.2016.78.pdf)</sup>；而在实践中，改进往往在相对较少的迭代之后便停止了。<sup>[4](https://ar5iv.labs.arxiv.org/html/1602.08254)</sup>

## 具体做法

实际使用者按以下流程操作 <sup>[2](http://www.cs.cmu.edu/~ninamf/papers/cluster-chapter.pdf)</sup><sup> • </sup><sup>[4](https://ar5iv.labs.arxiv.org/html/1602.08254)</sup>：

1. 选择 \( k \)，常用肘部法、间隙统计量或轮廓系数法。<sup>[10](https://www.vldb.org/pvldb/vol16/p1740-karras.pdf)</sup>
2. 选定 \( k \) 个初始中心作为种子，通常从数据点中均匀随机抽取。<sup>[1](https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf)</sup>
3. 把每个点指派给最近的中心，距离相同时任意选择其中一个中心。<sup>[4](https://ar5iv.labs.arxiv.org/html/1602.08254)</sup>
4. 用每个中心所辖点的均值替换该中心。<sup>[4](https://ar5iv.labs.arxiv.org/html/1602.08254)</sup>
5. 当满足某个准则时停止，并输出当前的中心。

常用的停止准则有：最大迭代次数、相邻两次迭代间质心的移动量、相邻两次迭代间簇的重新指派情况，以及相邻两次迭代间k均值代价的变化。<sup>[11](https://proceedings.mlr.press/v51/tang16b.pdf)</sup> 重新指派准则 RA(η) 在相邻两次迭代之间被重新指派的点所占比例低于 \( \eta \in (0,1) \) 时停止，适用于计算完整代价不现实的大规模场景。<sup>[11](https://proceedings.mlr.press/v51/tang16b.pdf)</sup> 由于初始化不当会产生空簇，使用者通过拆分大簇、或用离其所指派质心最远的点替换空质心 <sup>[12](https://arxiv.org/pdf/2603.20009)</sup> 来处理，并多次重启算法，保留代价最低的那次聚类结果。<sup>[13](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans)</sup>

最直白的实现方式在每次迭代中计算 \( \Theta(nk) \) 个距离，耗时 \( \Theta(ndk) \)。<sup>[4](https://ar5iv.labs.arxiv.org/html/1602.08254)</sup>

## 起源

该论文推导了使平均量化噪声功率最小的最优有限量化方案中，量化级与量化区间所须满足的必要条件。<sup>[14](https://web.stanford.edu/class/ee398a/handouts/papers/Lloyd%20-%20Least%20Squares%20Q%20in%20PCM.pdf)</sup> 在那种量化背景下，迭代从初始的沃罗诺伊剖分出发，把生成元重新定义为各自沃罗诺伊区域的质心，如此重复直到满足停止准则；若一个剖分的生成元正是其各自区域的质心，它就是质心沃罗诺伊剖分。<sup>[3](https://math.gmu.edu/~memelian/pubs/pdfs/DEJ_SIAM_lloyd.pdf)</sup>

詹姆斯·B. 麦昆（James B. MacQueen）于1967年提出了一个相关的k均值方法。<sup>[15](https://www.sciencedirect.com/science/article/abs/pii/S0020025522014633)</sup> 波拉德（Pollard）1982年的分析把k均值方法与分布上的量化联系起来，并在使代价最小的中心集合唯一时给出了一致性结果。<sup>[16](http://www.stat.yale.edu/%7Epollard/Papers/Pollard82ITT.pdf)</sup>

## 变体

劳埃德算法并未规定中心的初始位置 <sup>[7](https://doi.org/10.1109/tpami.2002.1017616)</sup>，而结果在很大程度上取决于这一选择。用 \( k \) 个数据点随机播种，可能导致解的质量任意地差。<sup>[2](http://www.cs.cmu.edu/~ninamf/papers/cluster-chapter.pdf)</sup> 大卫·阿瑟与谢尔盖·瓦西里维茨基（David Arthur and Sergei Vassilvitskii，2007）提出的k-means++初始化，以类似最远优先的平方距离加权来选取种子，被称为当前在理论上和实践上都最有效的方法。<sup>[1](https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf)</sup><sup> • </sup><sup>[17](https://doi.org/10.5555/1283383.1283494)</sup> 若 \( T \) 是k-means++选出的中心、\( T^* \) 是最优中心，则 \( E[\operatorname{cost}(T)] \leq \operatorname{cost}(T^*) \cdot O(\log k) \)，且随后的劳埃德迭代只会改进聚类。<sup>[18](https://cseweb.ucsd.edu/~dasgupta/291-geom/kmeans.pdf)</sup> 直白的k-means++实现仅播种一项就要把数据读大约 \( k \) 遍，这限制了可扩展性 <sup>[4](https://ar5iv.labs.arxiv.org/html/1602.08254)</sup>；在高维情形下，随机初始化质心可以与k-means++一样好。<sup>[12](https://arxiv.org/pdf/2603.20009)</sup>

加速类变体改变的是实现方式，而非迭代本身。卡农戈、芒特、内塔尼亚胡、皮亚特科、西尔弗曼与吴（Kanungo, Mount, Netanyahu, Piatko, Silverman, and Wu，2002）的过滤算法把点存进kd树，并按节点剪除候选中心；这一想法由阿尔萨卜提（Alsabti）及其同事、佩莱格与摩尔（Pelleg and Moore，他们把自己的版本称为黑名单算法）以及卡农戈及其同事各自独立发现。<sup>[7](https://doi.org/10.1109/tpami.2002.1017616)</sup> 另一些技术为更新步骤缓存充分统计量，即每个簇的向量和与点数，并利用几何关系避免指派步骤中的 \( nk \) 次距离计算。<sup>[5](https://cs.baylor.edu/~hamerly/papers/2014_pca_chapter_hamerly_drake.pdf)</sup> scikit-learn 用劳埃德算法或埃尔坎（Elkan）算法求解该问题，其 MiniBatchKMeans 从微批次进行增量式中心更新，在样本数 \( n > 10{,}000 \) 时很可能比批处理实现快得多。<sup>[13](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans)</sup>

## 应用

对于高维向量嵌入，原版劳埃德算法仍是各类聚类库中的事实选择：近来的各种变体在SIMD向量化、CPU/GPU友好性，以及在 \( d > 512 \)、簇数 \( k > 10{,}000 \) 的嵌入上的评测方面都还有欠缺。<sup>[12](https://arxiv.org/pdf/2603.20009)</sup> 一篇近期的系统论文提出了召回率提前终止（Early Termination by Recall，ETR），这是一种在聚类过程中监控召回率以决定何时停止的机制。<sup>[12](https://arxiv.org/pdf/2603.20009)</sup> 在理论方面，2026年的一篇PMLR论文主张，劳埃德的朴素K均值实际上等价于一种伪装的弗兰克-沃尔夫（Frank-Wolfe）方法，从而为其收敛性提供了新的理论框架。<sup>[19](https://proceedings.mlr.press/v300/pokojovy26a.html)</sup>

## 局限与替代方案

该方法的结构性局限已有充分记载。它要求事先指定簇数 \( k \)。<sup>[20](https://ar5iv.labs.arxiv.org/html/1209.1960)</sup> 它只能识别紧致、超球状且彼此分离良好的簇，这一限制可以借助马氏距离来处理超椭球状簇而有所缓解。<sup>[20](https://ar5iv.labs.arxiv.org/html/1209.1960)</sup> 由于使用欧氏距离的平方，它对噪声和离群点敏感，因为哪怕只有少数这样的点，也能显著影响其所在簇的均值；补救办法包括剪除离群点或改用L1（城市街区）距离。<sup>[20](https://ar5iv.labs.arxiv.org/html/1209.1960)</sup> 初始化不当会造成空簇、收敛变慢，以及更容易陷入糟糕的局部极小 <sup>[20](https://ar5iv.labs.arxiv.org/html/1209.1960)</sup>；而若把离群点选作种子，则可能产生只含单个点的簇。<sup>[21](https://nlp.stanford.edu/IR-book/html/htmledition/k-means-1.html)</sup> 劳埃德方法无论哪种变体，都只收敛到局部最优，并且对初始中心的选择敏感 <sup>[22](https://dl.acm.org/doi/10.1145/2395116.2395117)</sup>；而即使 \( k = 2 \)，精确k均值也是NP难的。<sup>[22](https://dl.acm.org/doi/10.1145/2395116.2395117)</sup>

k均值家族中有名有姓的替代方法包括哈蒂根-王（Hartigan–Wong）的K均值法和K中位数法。<sup>[23](https://link.springer.com/article/10.1007/s10994-021-06021-7)</sup>

## References

1. [k-means++: The Advantages of Careful Seeding (SODA)](https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf)
2. [Clustering chapter (Blum et al., CMU)](http://www.cs.cmu.edu/~ninamf/papers/cluster-chapter.pdf)
3. [Tessellations (on centroidal Voronoi tessellations)](https://math.gmu.edu/~memelian/pubs/pdfs/DEJ_SIAM_lloyd.pdf)
4. [Theoretical Analysis of the k-Means Algorithm – A Survey](https://ar5iv.labs.arxiv.org/html/1602.08254)
5. [Accelerating Lloyd's algorithm for k-means clustering (Hamerly & Drake)](https://cs.baylor.edu/~hamerly/papers/2014_pca_chapter_hamerly_drake.pdf)
6. [k-means Requires Exponentially Many Iterations Even in the Plane (Discrete & Computational Geometry)](https://link.springer.com/article/10.1007/s00454-011-9340-1)
7. [T. Kanungo and colleagues (2002). An efficient k-means clustering algorithm: analysis and implementation. IEEE Transactions on Pattern Analysis and Machine Intelligence.](https://doi.org/10.1109/tpami.2002.1017616)
8. [Modified K-means Algorithm with Local Optimality Guarantees (arXiv, 2025)](https://arxiv.org/html/2506.06990v2)
9. [The Complexity of the k-Means Method (ESA 2016)](https://drops.dagstuhl.de/storage/00lipics/lipics-vol057-esa2016/LIPIcs.ESA.2016.78/LIPIcs.ESA.2016.78.pdf)
10. [Marigold: Efficient k-means Clustering in High Dimensions (PVLDB v16)](https://www.vldb.org/pvldb/vol16/p1740-karras.pdf)
11. [On Lloyd's algorithm: new theoretical insights for clustering in practice (ICML 2016)](https://proceedings.mlr.press/v51/tang16b.pdf)
12. [Fast k-means for vector embeddings (arXiv preprint)](https://arxiv.org/pdf/2603.20009)
13. [KMeans, scikit-learn documentation](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans)
14. [Least Squares Quantization in PCM](https://web.stanford.edu/class/ee398a/handouts/papers/Lloyd%20-%20Least%20Squares%20Q%20in%20PCM.pdf)
15. [K-means clustering algorithms: A comprehensive review, variants analysis, and advances in the era of big data (Information Sciences)](https://www.sciencedirect.com/science/article/abs/pii/S0020025522014633)
16. [Quantization and the Method of k-Means (Pollard, 1982)](http://www.stat.yale.edu/%7Epollard/Papers/Pollard82ITT.pdf)
17. [David Arthur, Sergei Vassilvitskii (2007). k-means++: the advantages of careful seeding. .](https://doi.org/10.5555/1283383.1283494)
18. [Lecture 3, Algorithms for k-means clustering (Dasgupta, UCSD)](https://cseweb.ucsd.edu/~dasgupta/291-geom/kmeans.pdf)
19. [Lloyd's K-Means Clustering Algorithm is Frank-Wolfe in Disguise (PMLR v300, 2026)](https://proceedings.mlr.press/v300/pokojovy26a.html)
20. [A Comparative Study of Efficient Initialization Methods for the K-Means Clustering Algorithm](https://ar5iv.labs.arxiv.org/html/1209.1960)
21. [K-means, Introduction to Information Retrieval (Stanford)](https://nlp.stanford.edu/IR-book/html/htmledition/k-means-1.html)
22. [The effectiveness of Lloyd-type methods for the k-means problem](https://dl.acm.org/doi/10.1145/2395116.2395117)
23. [An empirical comparison between stochastic and deterministic centroid initialisation for K-means variations (Machine Learning)](https://link.springer.com/article/10.1007/s10994-021-06021-7)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Sep 30, 2026 · Last review: Sep 30, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Machine learning methods › Supervised, unsupervised, and semi-supervised learning › Clustering algorithms

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/lloyds-algorithm

English original: https://www.edgepedia.org/lloyds-algorithm
