# k近邻算法

k近邻算法（k-NN）是一种监督机器学习方法，它在一个选定的距离度量下，找出距离查询点最近的k个有标签训练样本，通过多数投票对查询点进行分类，或对这些样本的目标值取平均来预测一个连续值。它存储训练数据而非构建显式模型，这使它成为一种基于实例的、非泛化的方法。<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 最早表述 | Fix与Hodges，1951年，一份未发表的美国空军报告，研究未知分布总体的非参数判别<sup>[2](https://www.sciencedirect.com/science/article/abs/pii/S0378112711004014)</sup> |
| 经典分析 | Cover与Hart（1967），*IEEE信息论汇刊* 13(1)，第21–27页<sup>[3](https://gwern.net/doc/ai/nn/retrieval/1967-cover.pdf)</sup> |
| 误差保证 | 对于M个类别，R* ≤ R ≤ R*(2 − MR*/(M−1))；最近邻误差至多为贝叶斯误差的两倍<sup>[3](https://gwern.net/doc/ai/nn/retrieval/1967-cover.pdf)</sup> |
| 一致性 | Stone（1977）证明，当k随n缓慢增长时，例如k ≈ log(n)，k-NN规则是普遍贝叶斯一致的<sup>[4](https://www.sciencedirect.com/topics/computer-science/nearest-neighbors-classifier)</sup> |
| 搜索成本 | 对于D维中的N个样本，暴力搜索的规模为O[D N²]；树结构可将其降至O[D N log(N)]或更好<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup> |
| 维度限制 | KD树在D < 20时高效，但随着维度增长会退化至接近暴力搜索的成本，这是维数灾难的一种表现<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup> |
| 预处理 | 需要进行特征缩放：取值范围大的未缩放特征会主导距离计算<sup>[5](https://scikit-learn.org/stable/auto_examples/preprocessing/plot_scaling_importance.html)</sup> |

## 决策规则

给定由n个对（xᵢ, θᵢ）组成的训练集，其中xᵢ是测量向量，θᵢ是其类别标签，该规则在度量d下，将新观测x归入其k个最近邻中出现次数最多的类别。距离可以是任何度量；标准欧几里得距离是最常见的选择。<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup> 当k = 1时，该规则直接复制单个最近训练点的标签。当k > 1时，投票会平滑掉个别误标或含噪样本：较大的k抑制噪声的影响，但使分类边界变得不那么清晰。<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup>

**加权投票。** 基本形式给每个邻居相同的投票权。一种常见的改进是按每个邻居与查询点距离的倒数为其加权，使更近的样本贡献更大；也可以提供用户定义的距离加权函数。<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup>

**半径变体。** 基于半径的规则不使用固定的数量k，而是使用固定半径r内的所有邻居。这适合非均匀采样的数据，因为稀疏邻域中的点会使用较少的邻居；然而在高维参数空间中，由于维数灾难，半径方法会变得不那么有效。<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup>

## 理论保证

最近邻型规则的最早表述及其性质的主要早期分析归功于Evelyn Fix和Joseph L. Hodges，见于一份1951年未发表的美国空军报告，该报告后来于1977年和1989年重新发表；他们确立了在k/n → 0的序列下k-NN规则的一致性。<sup>[2](https://www.sciencedirect.com/science/article/abs/pii/S0378112711004014)</sup><sup> • </sup><sup>[3](https://gwern.net/doc/ai/nn/retrieval/1967-cover.pdf)</sup>

**Cover与Hart的界。** 当时在斯坦福的[Thomas M. Cover](https://www.edgepedia.org/zh/thomas-m-cover)和斯坦福研究所的Peter E. Hart于1967年发表了经典的有限样本和大样本分析。<sup>[3](https://gwern.net/doc/ai/nn/retrieval/1967-cover.pdf)</sup><sup> • </sup><sup>[6](https://www.centerconsulting.com/ai-library/papers/nearest-neighbor-cover-hart)</sup> 由于该规则忽略底层的联合分布，其错误概率R必须至少为贝叶斯误差R*，即所有使用该结构的决策规则中可达到的最小值。他们证明，对于M个类别和适当平滑的分布，大样本误差满足R* ≤ R ≤ R*(2 − MR*/(M−1))，因此对于任意数量的类别，最近邻误差的上界是贝叶斯误差的两倍。用他们的话说，无限样本集中的一半分类信息包含在最近邻中。<sup>[3](https://gwern.net/doc/ai/nn/retrieval/1967-cover.pdf)</sup> 他们还表明，单邻居规则在k-NN规则中是容许的：对某些分布而言，没有任何k ≠ 1的规则能在所有情况下取得更低的误差。<sup>[3](https://gwern.net/doc/ai/nn/retrieval/1967-cover.pdf)</sup>

**普遍一致性。** [Charles J. Stone](https://www.edgepedia.org/zh/charles-j-stone)于1977年证明，k-NN分类规则是普遍贝叶斯一致的：如果k随样本量n增长但增长足够缓慢，例如k ≈ log(n)使得k/n → 0，则该规则的误差对基本上任何底层分布都收敛到贝叶斯误差。<sup>[4](https://www.sciencedirect.com/topics/computer-science/nearest-neighbors-classifier)</sup><sup> • </sup><sup>[7](https://arxiv.org/abs/2003.00894)</sup> 这一结果使k-NN跻身于无需分布假设即可确定渐近行为的方法之列。

## 加速搜索

分类需要找到k个最近的训练点，快速邻居搜索是一个活跃的研究领域。朴素方法计算查询与每个训练样本之间的距离；对于D维中的N个样本，所有对的计算规模为O[D N²]，随着N增长，暴力搜索变得不可行。<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup>

**KD树。** KD树是一种沿数据轴递归划分参数空间的二叉树，它利用聚合距离信息：如果点A远离B且B靠近C，则无需计算即可知A和C相距很远。构建不需要D维距离计算，一次查询需要约O[log(N)]次距离计算。这在低维（大致D < 20）下效果良好，但随着D增大，成本会上升至接近O[DN]，且树的额外开销可能使查询比暴力搜索更慢。<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup>

**球树。** 为解决KD树在高维中的低效问题，球树将数据划分为由质心C和半径r定义的嵌套超球体。到质心的一次距离计算可通过三角不等式界定到该节点中每个点的距离。构建成本更高，但在高度结构化的数据上，即使维度很高，该结构也可能优于KD树，其性能强烈依赖于数据的结构。<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup>

**近似搜索。** 在高维中，精确搜索在根本层面上受到维数灾难的影响，理论表明了硬性限制：汉明空间中高维k-NN的多项式空间索引每次查询需要Ω(k)次块读取。<sup>[8](https://www.sciencedirect.com/science/article/pii/S0898122112006426)</sup><sup> • </sup><sup>[9](https://arxiv.org/abs/2002.04870)</sup> 近似最近邻（ANN）算法以精度换取速度，返回一个距离至多为真实最小值c倍的点。麻省理工学院的Piotr Indyk和Tal Wagner给出了一种(1+ε)-近似结构，对于整数坐标点仅使用O(ε⁻² n log(n) log(1/ε))比特的空间，改进了此前通过Johnson–Lindenstrauss降维获得的O(ε⁻² n log(n)²)界。<sup>[10](https://proceedings.mlr.press/v75/indyk18a.html)</sup> 实用的ANN方法包括带随机旋转的随机分治方案和局部图搜索，这些方法在约20维时表现极佳，到约100维时明显退化。<sup>[11](https://pmc.ncbi.nlm.nih.gov/articles/PMC3179075/)</sup>

## 实际使用

**选择k与加权。** 最优k高度依赖于数据。较大的k降低噪声敏感性，代价是类别边界更模糊；软件中常见的默认值是k = 5。<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup><sup> • </sup><sup>[12](https://itl.nist.gov/div898/software/dataplot/refman1/auxillar/knn_plot.htm)</sup> 当各邻居到查询点的距离差异很大时，距离加权投票会有所帮助。<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup>

**特征缩放。** 由于预测完全依赖于距离，k-NN需要归一化的特征。在scikit-learn的Wine数据集示例中，变量脯氨酸在0到1,000之间变化，而色调在1到10之间变化，因此未缩放的距离由脯氨酸主导，色调实际上被忽略；将两个特征标准化到大致−3到3的范围会产生完全不同、更均衡的决策边界。<sup>[5](https://scikit-learn.org/stable/auto_examples/preprocessing/plot_scaling_importance.html)</sup>

**距离选择。** 一篇关于k-NN距离度量的综述指出，该分类器是最简单、最常见的方法之一，但其性能可与更复杂的分类器相竞争，且距离度量的选择会实质性影响结果。<sup>[13](https://pubmed.ncbi.nlm.nih.gov/31411491/)</sup> [欧几里得距离](https://www.edgepedia.org/zh/euclidean-distance)的替代方案包括曼哈顿距离、闵可夫斯基距离、切比雪夫距离、余弦距离、汉明距离和马哈拉诺比斯距离，可在标准实现中选择。<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup><sup> • </sup><sup>[12](https://itl.nist.gov/div898/software/dataplot/refman1/auxillar/knn_plot.htm)</sup>

**应用。** k-NN广泛用于医学图像分析，通常作用于卷积神经网络提取的特征。一种用于BraTS 2015和BraTS 2017数据集中MRI脑肿瘤分类的CNN-KNN混合方法报告了96.25%的准确率，CNN-KNN混合流程也已应用于肺结节和COVID-19 CT图像分类。<sup>[14](https://link.springer.com/article/10.1007/s10462-024-10814-2)</sup> 最近邻技术还进入了林业等领域，用于从参考样地估计森林属性。<sup>[2](https://www.sciencedirect.com/science/article/abs/pii/S0378112711004014)</sup>

**局限。** 该方法的成本在于内存和查询时间：它存储所有训练数据，预测成本随数据集增长。其在高维数据中有据可查的弱点、对仔细预处理的依赖，以及对噪声和类别不平衡的脆弱性，使其在某些复杂分类任务中成为次优选择。<sup>[1](https://scikit-learn.org/stable/modules/neighbors.html)</sup><sup> • </sup><sup>[15](https://www.nature.com/articles/s41598-025-31562-5_reference.pdf)</sup>

## References

1. [1.6. Nearest Neighbors, scikit-learn documentation](https://scikit-learn.org/stable/modules/neighbors.html)
2. [Estimating forest attribute parameters for small areas using nearest neighbors techniques, Forest Ecology and Management](https://www.sciencedirect.com/science/article/abs/pii/S0378112711004014)
3. [T. Cover, P. Hart (1967). Nearest Neighbor Pattern Classification, IEEE Transactions on Information Theory 13(1), 21–27](https://gwern.net/doc/ai/nn/retrieval/1967-cover.pdf)
4. [Nearest Neighbors Classifier, ScienceDirect Topics (Stone 1977 theorem)](https://www.sciencedirect.com/topics/computer-science/nearest-neighbors-classifier)
5. [Importance of Feature Scaling, scikit-learn example](https://scikit-learn.org/stable/auto_examples/preprocessing/plot_scaling_importance.html)
6. [Nearest Neighbor Pattern Classification, CenterConsulting AI library](https://www.centerconsulting.com/ai-library/papers/nearest-neighbor-cover-hart)
7. [Universal consistency of the k-NN rule in metric spaces and Nagata dimension, arXiv](https://arxiv.org/abs/2003.00894)
8. [Is the k-NN classifier in high dimensions affected by the curse of dimensionality?, Applied Mathematics and Computation](https://www.sciencedirect.com/science/article/pii/S0898122112006426)
9. [On the I/O complexity of the k-nearest neighbor problem, arXiv](https://arxiv.org/abs/2002.04870)
10. [P. Indyk, T. Wagner (2018). Approximate Nearest Neighbors in Limited Space, COLT 2018, PMLR 75:2012–2036](https://proceedings.mlr.press/v75/indyk18a.html)
11. [Randomized approximate nearest neighbors algorithm, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC3179075/)
12. [K Nearest Neighbors Classification Plot, NIST Dataplot reference manual](https://itl.nist.gov/div898/software/dataplot/refman1/auxillar/knn_plot.htm)
13. [Effects of Distance Measure Choice on K-Nearest Neighbor Classifier Performance: A Review, PubMed](https://pubmed.ncbi.nlm.nih.gov/31411491/)
14. [Efficient artificial intelligence approaches for medical image processing in healthcare, Artificial Intelligence Review](https://link.springer.com/article/10.1007/s10462-024-10814-2)
15. [Scientific Reports reference list citing KNN limitations](https://www.nature.com/articles/s41598-025-31562-5_reference.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

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

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/k-nearest-neighbors-algorithm

English original: https://www.edgepedia.org/k-nearest-neighbors-algorithm
