# 朴素贝叶斯分类器

**朴素贝叶斯分类器**是一种监督学习算法，它在“朴素”假设——即给定类别变量后，每一对特征都条件独立——之下应用贝叶斯定理。尽管做了这样的简化，朴素贝叶斯分类器在许多现实场景中仍然表现良好，尤以[文档分类](https://www.edgepedia.org/zh/document-classification)和垃圾邮件过滤著称，并且至今仍是文本分类中的标准基线模型。<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 决策规则 | \( \hat{y} = \arg\max_y \; P(y) \prod_{i=1}^{n} P(x_i \mid y) \)，参数由最大后验（MAP）估计得出<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup> |
| 主要变体 | 高斯（连续特征）、多项式（词频或 tf-idf）、伯努利（词是否出现）、补集以及类别朴素贝叶斯<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup> |
| 平滑 | 取 \( \alpha = 1 \) 的加一（拉普拉斯）平滑，或取 \( \alpha < 1 \) 的利德斯通平滑，可避免未出现的特征值概率为零<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup> |
| 训练数据需求 | 只需少量训练数据即可估计所需参数<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup> |
| 速度 | 对多项式文本模型，训练为 \( \Theta(\lvert \mathbb{D} \rvert L_{ave} + \lvert \mathbb{C} \rvert \lvert V \rvert) \)，测试为 \( \Theta(\lvert \mathbb{C} \rvert M_a) \)，时间复杂度与单次扫描数据所需的时间呈线性关系<sup>[2](https://www-nlp.stanford.edu/IR-book/html/htmledition/naive-bayes-text-classification-1.html)</sup> |
| 已知弱点 | 是不错的分类器，却是糟糕的估计器：`predict_proba` 给出的类别概率不宜过于当真<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup> |
| 扩展性 | `partial_fit` 为 MultinomialNB、BernoulliNB 和 GaussianNB 支持核外增量拟合<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup> |

## 工作原理

给定类别变量 \( y \) 和依赖特征向量 \( x_1 \) 至 \( x_n \)，[贝叶斯定理](https://www.edgepedia.org/zh/bayes-theorem)给出

\[ P(y \mid x_1, \dots, x_n) = \frac{P(y)\, P(x_1, \dots, x_n \mid y)}{P(x_1, \dots, x_n)}. \]

朴素假设是：给定类别后，每个特征都与其他特征条件独立，

\[ P(x_i \mid y, x_1, \dots, x_{i-1}, x_{i+1}, \dots, x_n) = P(x_i \mid y), \]

这将联合似然简化为若干一维项的乘积。由于对给定输入而言分母 \( P(x_1, \dots, x_n) \) 为常数，分类便归结为

\[ \hat{y} = \arg\max_y \; P(y) \prod_{i=1}^{n} P(x_i \mid y), \]

其中 \( P(y) \) 与 \( P(x_i \mid y) \) 由最大后验估计得出，\( P(y) \) 取类别 \( y \) 在训练集中的相对频率。<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup>

独立性假设正是模型易于处理的原因。将类条件特征分布解耦，意味着每个分布都可作为一维分布独立估计，这缓解了[维数灾难](https://www.edgepedia.org/zh/curse-of-dimensionality)带来的问题，也使学习器和分类器相比更精细的方法极为迅速。<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup>

## 变体

不同的朴素贝叶斯分类器主要区别在于它们对 \( P(x_i \mid y) \) 分布所作的假设。<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup>

**高斯朴素贝叶斯**假设每个特征的似然为高斯分布，

\[ P(x_i \mid y) = \frac{1}{\sqrt{2\pi\sigma_y^2}} \exp\left(-\frac{(x_i - \mu_y)^2}{2\sigma_y^2}\right), \]

其中 \( \sigma_y \) 与 \( \mu_y \) 由最大似然估计。它无需离散化即可处理连续特征，方法是在每个类别内估计每个特征的均值与方差。<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup><sup> • </sup><sup>[3](https://www.munotes.in/notes/BSc-Computer-Science/TY-BSc-Computer-Science/Semester-5/Artificial-Intelligence/the-naive-bayes-classifier/)</sup> 在 scikit-learn 中，`GaussianNB` 还会将最大特征方差的一部分（默认 \( 1 \times 10^{-9} \)）加到所有方差上以保证计算稳定，并通过 `partial_fit` 支持在线更新。<sup>[4](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html?highlight=gaussiannb)</sup>

**多项式朴素贝叶斯**是文本分类的两种经典变体之一，其数据通常是词频向量（tf-idf 向量在实践中也同样适用）。每个类别 \( y \) 由词表上的向量 \( \theta_y = (\theta_{y1}, \ldots, \theta_{yn}) \) 参数化，通过平滑的相对频率计数估计：

\[ \hat{\theta}_{yi} = \frac{N_{yi} + \alpha}{N_y + \alpha n}, \]

其中 \( N_{yi} \) 是特征 \( i \) 在类别 \( y \) 的所有训练样本中的计数，\( N_y \) 是该类别的总计数，\( \alpha \ge 0 \) 是平滑先验。取 \( \alpha = 1 \) 即拉普拉斯平滑；\( \alpha < 1 \) 即利德斯通平滑。<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup>

**伯努利朴素贝叶斯**是另一种经典文本变体：每个特征是词表中的词是否出现在文档中的二值指示。在此模型下，词 \( i \) 出现在某类别中的概率的最大似然估计，就是包含该词的该类文档数除以该类文档总数。<sup>[5](https://inst.eecs.berkeley.edu/~cs188/textbook/ml/naive-bayes.html)</sup>

**补集朴素贝叶斯（CNB）**针对不平衡数据集改进了多项式算法，它从每个类别的补集——即所有不属于该类的文档——的统计量中计算模型权重。其发明者通过实验表明，CNB 的参数估计比多项式朴素贝叶斯更稳定，且在文本分类任务上 CNB 经常胜出，往往优势相当明显。<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup>

**类别朴素贝叶斯**假设每个特征各有自己的类别分布，估计 \( P(x_i = t \mid y = c) = (N_{tic} + \alpha)/(N_c + \alpha n_i) \)，其中 \( N_{tic} \) 统计特征 \( i \) 的类别 \( t \) 在类别 \( c \) 中出现的次数，\( n_i \) 是该特征的类别数。<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup>

## 零频率问题与平滑

当某个特征值在训练中从未与某个类别共现时，最大似然估计就会失效。在文本分类中，若词项“WTO”在训练期间只出现在中国文档中，则 \( \hat{P}(\text{WTO} \mid \text{UK}) = 0 \)，而单句文档“英国是 WTO 的成员”对英国类别的条件概率便为零，因为乘积会乘上每个词项的条件概率。这个零无法通过该类别其他更强的证据消除。估计为零源于稀疏性：训练数据永远不够大，不足以充分体现稀有事件的频率。<sup>[2](https://www-nlp.stanford.edu/IR-book/html/htmledition/naive-bayes-text-classification-1.html)</sup>

**拉普拉斯平滑**通过加入伪计数来解决此问题。设平滑强度为 \( k \)，某结果在规模为 \( N \)、可能取值有 \( \lvert X \rvert \) 个的样本中出现 \( count(x) \) 次，则估计为

\[ P_{LAP,k}(x) = \frac{count(x) + k}{N + k\lvert X \rvert}, \qquad P_{LAP,k}(x \mid y) = \frac{count(x, y) + k}{count(y) + k\lvert X \rvert}. \]

从概念上讲，模型表现得就像它已见过每个结果额外 \( k \) 个实例。两个极限情形颇具启发性：\( k = 0 \) 恢复最大似然估计，\( k = \infty \) 使每个结果等可能，\( P_{LAP,\infty}(x) = 1/\lvert X \rvert \)。合适的 \( k \) 值是一个超参数，通常依据验证数据反复试验选取。<sup>[5](https://inst.eecs.berkeley.edu/~cs188/textbook/ml/naive-bayes.html)</sup> 加一平滑可解释为均匀先验——每个词项对每个类别各出现一次——再由训练证据加以更新。<sup>[2](https://www-nlp.stanford.edu/IR-book/html/htmledition/naive-bayes-text-classification-1.html)</sup>

## 数值稳定性

将许多条件概率（每个词元位置一个）相乘，可能造成浮点数下溢。因此实现时改为相加概率的对数而非相乘；由于 \( \log(xy) = \log(x) + \log(y) \) 且对数单调，对数概率得分最高的类别仍是概率最大的类别。在这种形式下，每个 \( \log \hat{P}(w \mid c) \) 充当权重，表明该词对类别的指示性有多好，而对数先验则加权类别的相对频率。<sup>[2](https://www-nlp.stanford.edu/IR-book/html/htmledition/naive-bayes-text-classification-1.html)</sup>

## 用于文本分类的朴素贝叶斯

多项式事件模型将文档 \( d_i \) 表示为词表 \( V \) 上的词频向量，并在假设词与词独立的前提下选择类别

\[ c^*(d_i) = \arg\max_j \; p(c_j) \prod_{t=1}^{\lvert V \rvert} p(w_t \mid c_j)^{n(w_t, d_i)}, \]

其中 \( n(w_t, d_i) \) 是词 \( w_t \) 在文档中出现的次数。<sup>[6](https://aclanthology.org/anthology-files/pdf/P/P04/P04-3024.pdf)</sup> 机器学习与信息检索领域的研究者安德鲁·麦卡勒姆（Andrew McCallum）与卡迈勒·尼加姆（Kamal Nigam）在五个文本语料上比较了多项式与多变量伯努利事件模型，发现多变量伯努利模型在小词表规模下表现良好，而多项式模型在较大词表规模下通常更优，在任意词表规模下平均比多变量[伯努利模型](https://www.edgepedia.org/zh/bernoulli-model)降低 27% 的误差。<sup>[7](https://aaai.org/papers/041-ws98-05-007/)</sup>

多项式模型的高效是其广受欢迎的主要原因。计算参数花费 \( \Theta(\lvert \mathbb{C} \rvert \lvert V \rvert) \) 外加一遍训练集扫描，而对含 \( M_a \) 个不同词项的测试文档，分类花费 \( \Theta(\lvert \mathbb{C} \rvert M_a) \)。由于 \( \lvert \mathbb{C} \rvert \lvert V \rvert \) 通常小于训练集规模，训练与测试都随扫描数据所需时间线性增长，《信息检索导论》（*Introduction to Information Retrieval*）一书称此为最优时间复杂度。<sup>[2](https://www-nlp.stanford.edu/IR-book/html/htmledition/naive-bayes-text-classification-1.html)</sup>

## 优势与局限

对大多数真实数据而言独立性假设并不成立，但分类器往往仍表现良好。马格努斯·埃克达尔（Magnus Ekdahl）与数理统计学教授蒂莫·科斯基（Timo Koski）的理论分析表明，朴素贝叶斯的性能取决于特征与标签之间函数依赖的程度，他们给出了一个充分条件：在此条件下，与最优分类器相比，朴素近似的正确分类概率损失为零。<sup>[8](https://www.jmlr.org/beta/papers/v7/ekdahl06a.html)</sup> 在经验层面，一项研究观察到决策树、1-最近邻和支持向量机利用了特征间的依赖关系，当特征高度相关时误差下降，而朴素贝叶斯的分类误差似乎与该相关性无关。<sup>[9](https://www.jmlr.org/papers/volume11/ojala10a/ojala10a.pdf)</sup>

实践中的主要注意事项是概率估计和平滑选择。尽管朴素贝叶斯以不错的分类器著称，它也是众所周知的糟糕估计器，因此概率输出不应过于当真。<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup> 不做平滑则模型脆弱：单个未见的特征值就会迫使类别似然为零，可能彻底破坏分类器。<sup>[10](https://github.com/dharminnagar/ML-notes/blob/main/Laplace-Smoothing.md)</sup> 平滑选择还会与事件模型相互作用；一项重复研究发现，麦卡勒姆与尼加姆所建议的拉普拉斯先验反而降低了其分类器的性能。<sup>[11](https://aclanthology.org/anthology-files/anthology-files/pdf/E/E03/E03-1059.pdf)</sup>

## 扩展到大数据

朴素贝叶斯模型可以处理完整训练集无法装入内存的分类问题。在 scikit-learn 中，`MultinomialNB`、`BernoulliNB` 和 `GaussianNB` 提供 `partial_fit` 方法用于核外增量拟合；首次调用必须传入所有预期类别标签的列表，且所有朴素贝叶斯分类器都支持样本加权。由于每次 `partial_fit` 调用都带有计算开销，文档建议数据块应尽可能大，以可用内存为限。<sup>[1](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup>

## References

1. [1.9. Naive Bayes, scikit-learn 1.9.1 documentation](https://scikit-learn.org/stable/modules/naive_bayes.html)
2. [Naive Bayes text classification, Introduction to Information Retrieval (Manning, Raghavan, Schütze), Stanford NLP](https://www-nlp.stanford.edu/IR-book/html/htmledition/naive-bayes-text-classification-1.html)
3. [The Naive Bayes Classifier, B.Sc. Computer Science Semester 5 notes, munotes](https://www.munotes.in/notes/BSc-Computer-Science/TY-BSc-Computer-Science/Semester-5/Artificial-Intelligence/the-naive-bayes-classifier/)
4. [sklearn.naive_bayes.GaussianNB, scikit-learn API documentation](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html?highlight=gaussiannb)
5. [9.2 Naive Bayes, Introduction to Artificial Intelligence, UC Berkeley CS188](https://inst.eecs.berkeley.edu/~cs188/textbook/ml/naive-bayes.html)
6. [A New Feature Selection Score for Multinomial Naive Bayes Text Classification Based on KL-Divergence, ACL](https://aclanthology.org/anthology-files/pdf/P/P04/P04-3024.pdf)
7. [Andrew McCallum and Kamal Nigam. A Comparison of Event Models for Naive Bayes Text Classification. AAAI Workshop](https://aaai.org/papers/041-ws98-05-007/)
8. [Magnus Ekdahl and Timo Koski (2006). Bounds for the Loss in Probability of Correct Classification Under Model Based Approximation. Journal of Machine Learning Research 7: 2449–2480](https://www.jmlr.org/beta/papers/v7/ekdahl06a.html)
9. [Ojala et al. (2010). A comparative study of classifier performance, Journal of Machine Learning Research 11](https://www.jmlr.org/papers/volume11/ojala10a/ojala10a.pdf)
10. [Laplace Smoothing notes, dharminnagar/ML-notes, GitHub](https://github.com/dharminnagar/ML-notes/blob/main/Laplace-Smoothing.md)
11. [Text classification with multinomial naive Bayes, ACL E03-1059](https://aclanthology.org/anthology-files/anthology-files/pdf/E/E03/E03-1059.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/naive-bayes-classifier

English original: https://www.edgepedia.org/naive-bayes-classifier
