# 词袋模型

词袋模型是一种文本表示方法，它将文档简化为其所含词项的多重集：词序和句法被丢弃，只保留每个词项的出现次数<sup>[1](https://nlp.stanford.edu/IR-book/html/htmledition/term-frequency-and-weighting-1.html)</sup><sup> • </sup><sup>[2](https://technav.ieee.org/topic/bag-of-words-model/)</sup>。在这种视角下，“玛丽比约翰快”和“约翰比玛丽快”这两个句子会产生完全相同的表示<sup>[1](https://nlp.stanford.edu/IR-book/html/htmledition/term-frequency-and-weighting-1.html)</sup>。该模型是经典信息检索评分、文本分类以及许多自然语言处理流程的基础，它将变长文本转换为机器学习算法可以使用的定长数值向量<sup>[3](https://scikit-learn.org/1.1/modules/feature_extraction.html)</sup>。

| 关键事实 | 详情 |
|---|---|
| 定义 | 文档由其词项及词项计数表示；词序和句法被忽略<sup>[1](https://nlp.stanford.edu/IR-book/html/htmledition/term-frequency-and-weighting-1.html)</sup> |
| 基本权重 | 词频 \(\mathrm{tf}_{t,d}\)，即词项 \(t\) 在文档 \(d\) 中的出现次数<sup>[1](https://nlp.stanford.edu/IR-book/html/htmledition/term-frequency-and-weighting-1.html)</sup> |
| 标准加权 | \(\mathrm{tf\text{-}idf}_{t,d} = \mathrm{tf}_{t,d} \times \mathrm{idf}_t\)，在少数文档中频繁出现的词项权重最高，在几乎所有文档中都出现的词项权重最低<sup>[4](https://nlp.stanford.edu/IR-book/html/htmledition/tf-idf-weighting-1.html)</sup> |
| 相似度度量 | 文档向量之间的余弦相似度，可补偿文档长度的差异<sup>[5](https://nlp.stanford.edu/IR-book/html/htmledition/dot-products-1.html)</sup> |
| 稀疏性 | 文档-词项矩阵通常超过99%为零；10,000篇短文档可能产生约100,000词的词汇表，而每篇文档只使用100到1,000个独特词<sup>[3](https://scikit-learn.org/1.1/modules/feature_extraction.html)</sup> |
| 主要局限 | 一元词袋无法捕捉短语、多词表达或词序依赖关系<sup>[3](https://scikit-learn.org/1.1/modules/feature_extraction.html)</sup> |
| 常见扩展 | n元词袋，对连续词的配对或更长片段进行计数，以恢复部分局部顺序信息<sup>[3](https://scikit-learn.org/1.1/modules/feature_extraction.html)</sup><sup> • </sup><sup>[6](https://aclanthology.org/E17-2068.pdf)</sup> |

## 构建

构建词袋表示涉及三个步骤：对字符串进行分词并为每个可能的词元分配一个整数标识符，统计每个文档中词元的出现次数，以及可选地对计数进行归一化或加权<sup>[3](https://scikit-learn.org/1.1/modules/feature_extraction.html)</sup>。结果是一个文档-词项矩阵，每篇文档一行，语料库词汇表中的每个词元一列<sup>[3](https://scikit-learn.org/1.1/modules/feature_extraction.html)</sup>。

词项最简单的权重是其在文档中的原始计数，这种方案称为词频，记作 \(\mathrm{tf}_{t,d}\)<sup>[1](https://nlp.stanford.edu/IR-book/html/htmledition/term-frequency-and-weighting-1.html)</sup>。任何将 \(t\) 在 \(d\) 中的出现次数映射为正实数值的函数都可以作为权重，所得的向量充当文档的定量摘要<sup>[1](https://nlp.stanford.edu/IR-book/html/htmledition/term-frequency-and-weighting-1.html)</sup>。

## 加权：从计数到tf-idf

原始计数将所有词项视为同等重要，这对检索来说是个问题：在一个关于汽车行业的文档集中，“汽车”一词几乎出现在每篇文档中，几乎没有区分能力<sup>[7](https://nlp.stanford.edu/IR-book/pdf/irbookonlinereading.pdf)</sup>。标准的修正方法是逆文档频率加权，基于文档频率 \(\mathrm{df}_t\)，即文档集中包含词项 \(t\) 的文档数<sup>[8](https://nlp.stanford.edu/IR-book/html/htmledition/inverse-document-frequency-1.html)</sup>。

将两者结合即得到tf-idf权重：

\[ \mathrm{tf\text{-}idf}_{t,d} = \mathrm{tf}_{t,d} \times \mathrm{idf}_t \]

一种常见的具体形式为

\[ w_{t,d} = (1 + \log \mathrm{tf}_{t,d}) \cdot \log \frac{N}{\mathrm{df}_t} \]

其中 \(N\) 是文档集中的文档数<sup>[9](https://nlp.stanford.edu/IR-book/essir2011/pdf/vspace.pdf)</sup>。当一个词项在少量文档中多次出现时，其tf-idf权重最高；当它出现次数较少或在许多文档中出现时，权重较低；当它几乎出现在所有文档中时，权重最低<sup>[4](https://nlp.stanford.edu/IR-book/html/htmledition/tf-idf-weighting-1.html)</sup>。

## 向量空间视角

有了每个词项的权重，每篇文档就变成一个向量，每个词典词项对应一个分量，而文档集则成为每个词项对应一个坐标轴的空间中的一组向量<sup>[4](https://nlp.stanford.edu/IR-book/html/htmledition/tf-idf-weighting-1.html)</sup><sup> • </sup><sup>[5](https://nlp.stanford.edu/IR-book/html/htmledition/dot-products-1.html)</sup>。这种表示丢失了词项的相对顺序，而这正是词袋属性的本质<sup>[5](https://nlp.stanford.edu/IR-book/html/htmledition/dot-products-1.html)</sup>。

由于词项分布相同但长度不同的两篇文档在原始向量上会显示出很大的差异，标准的相似度度量是两个向量的余弦相似度，即点积除以它们欧几里得长度的乘积<sup>[5](https://nlp.stanford.edu/IR-book/html/htmledition/dot-products-1.html)</sup>。查询可以被视为一篇非常短的文档，因此同样的余弦分数可以对文档相对于查询进行排序，即使文档不包含每个查询词项，也可以获得高分<sup>[10](https://nlp.stanford.edu/IR-book/html/htmledition/queries-as-vectors-1.html)</sup>。

## 在文本分类中的使用

在监督式文本分类中，词袋向量是分类器的输入。朴素贝叶斯的多项式变体——文本的两种经典朴素贝叶斯变体之一——以词计数向量作为输入，尽管tf-idf向量在实践中也被证明效果良好<sup>[11](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup>。伯努利变体则要求记录每个词项是否出现的二元特征向量，并且它明确惩罚指示某个类别的词项的不出现，而多项式变体忽略了这一点；在文档较短的数据集上，它可能表现更好<sup>[11](https://scikit-learn.org/stable/modules/naive_bayes.html)</sup>。

在20 newsgroups数据集上的一个实例展示了典型的规模和性能。对来自四个类别的2,257篇帖子进行分词，产生了一个2,257篇文档×35,788个特征的计数矩阵。计数、tf-idf变换和多项式朴素贝叶斯的流水线在留出帖子上达到了83.5%的准确率；将分类器替换为线性支持向量机后，准确率提升至91.3%<sup>[12](https://scikit-learn.org/0.20/tutorial/text_analytics/working_with_text_data.html)</sup>。线性模型是词袋特征的常见搭档，因为该表示是高维且稀疏的，而稀疏向量使存储和线性代数运算都变得可行<sup>[3](https://scikit-learn.org/1.1/modules/feature_extraction.html)</sup>。在20 newsgroups的一个子集上，tf-idf向量在超过30,000维的空间中平均每篇文档约有159个非零分量，非零特征不到0.5%<sup>[13](https://scikit-learn.org/stable/datasets/real_world.html)</sup>。

尽管简单，该表示已被证明在广泛的自然语言处理任务中有效，包括词义消歧、文本分类和情感分析<sup>[14](https://www-nlp.stanford.edu/~wcmac/papers/nli-diss.pdf)</sup>。预处理选择也很重要：一项系统研究在四个基准语料库上对五种或六种基本预处理方法的所有组合进行了测试，使用贝叶斯网络、一种支持向量机变体和随机森林配合词袋一元词，发现特定的预处理组合在特定数据集上显著改善了分类结果<sup>[15](https://journals.plos.org/plosone/article?id=10.1371%2Fjournal.pone.0232525)</sup>。

## 局限与扩展

核心局限是顺序的丢失。一元词的集合无法捕捉短语和多词表达，模型也不考虑拼写错误或词派生<sup>[3](https://scikit-learn.org/1.1/modules/feature_extraction.html)</sup>。两种部分补救措施仍在计数框架内：

- **词n元语法。** 对二元词或更长的连续词片段进行计数，可以保留一些局部位置信息。例如，fastText分类器使用n元词袋作为额外特征，因为显式建模词序在计算上代价高昂，而这取得了与顺序感知方法相当的结果<sup>[6](https://aclanthology.org/E17-2068.pdf)</sup>。
- **字符n元语法。** 对[字符](https://www.edgepedia.org/zh/character-computing)序列进行计数使表示对拼写错误和派生具有鲁棒性；对于语料库 \([\text{words}, \text{wprds}]\)，一元词模型看到的是两篇完全不同的文档，而字符二元语法表示发现这两篇文档在8个特征中有4个匹配<sup>[3](https://scikit-learn.org/1.1/modules/feature_extraction.html)</sup>。

即使使用n元语法，词袋和n元词袋表示也破坏了文档的大部分内部结构以及该结构所承载的意义，因此需要自然语言理解的任务需要将句子和段落结构考虑在内的模型<sup>[3](https://scikit-learn.org/1.1/modules/feature_extraction.html)</sup>。连续表示如word2vec和doc2vec将词和文档编码为稠密向量，是文档分类研究中与词袋进行比较的主要替代方案<sup>[16](https://arxiv.org/abs/1707.01321)</sup>。

## References

1. [Term frequency and weighting, Introduction to Information Retrieval (Manning, Raghavan, Schütze), Stanford NLP](https://nlp.stanford.edu/IR-book/html/htmledition/term-frequency-and-weighting-1.html)
2. [Bag of words model, IEEE Technology Navigator](https://technav.ieee.org/topic/bag-of-words-model/)
3. [6.2. Feature extraction, scikit-learn 1.1.3 documentation](https://scikit-learn.org/1.1/modules/feature_extraction.html)
4. [Tf-idf weighting, Introduction to Information Retrieval, Stanford NLP](https://nlp.stanford.edu/IR-book/html/htmledition/tf-idf-weighting-1.html)
5. [Dot products, Introduction to Information Retrieval, Stanford NLP](https://nlp.stanford.edu/IR-book/html/htmledition/dot-products-1.html)
6. [Joulin, A. et al. (2016). Bag of Tricks for Efficient Text Classification, EACL 2017](https://aclanthology.org/E17-2068.pdf)
7. [Introduction to Information Retrieval, full text PDF, Stanford NLP](https://nlp.stanford.edu/IR-book/pdf/irbookonlinereading.pdf)
8. [Inverse document frequency, Introduction to Information Retrieval, Stanford NLP](https://nlp.stanford.edu/IR-book/html/htmledition/inverse-document-frequency-1.html)
9. [IIR 6&7: Vector Space Model, lecture slides, Stanford NLP](https://nlp.stanford.edu/IR-book/essir2011/pdf/vspace.pdf)
10. [Queries as vectors, Introduction to Information Retrieval, Stanford NLP](https://nlp.stanford.edu/IR-book/html/htmledition/queries-as-vectors-1.html)
11. [1.9. Naive Bayes, scikit-learn 1.9.1 documentation](https://scikit-learn.org/stable/modules/naive_bayes.html)
12. [Working With Text Data, scikit-learn 0.20.4 tutorial](https://scikit-learn.org/0.20/tutorial/text_analytics/working_with_text_data.html)
13. [9.2. Real world datasets, scikit-learn 1.9.1 documentation](https://scikit-learn.org/stable/datasets/real_world.html)
14. [MacCartney, B. Natural Language Inference, Stanford dissertation](https://www-nlp.stanford.edu/~wcmac/papers/nli-diss.pdf)
15. [The influence of preprocessing on text classification using a bag-of-words representation, PLOS One (2020)](https://journals.plos.org/plosone/article?id=10.1371%2Fjournal.pone.0232525)
16. [The Influence of Feature Representation of Text on the Performance of Document Classification, arXiv:1707.01321](https://arxiv.org/abs/1707.01321)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › 语言与视觉人工智能 › 自然语言处理 › NLP任务与方法 › 文本分类与归类

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/bag-of-words-model

English original: https://www.edgepedia.org/bag-of-words-model
