1.9. 朴素贝叶斯#
朴素的Bayes方法是一组监督学习算法,基于应用Bayes定理,并假设给定类变量的值,每对特征之间存在条件独立性的“朴素”假设。给定类变量,Bayes定理陈述了以下关系 \(y\) 和相关特征载体 \(x_1\) 通过 \(x_n\) , :
使用天真的条件独立性假设
为所有 \(i\) ,这种关系被简化为
以来 \(P(x_1, \dots, x_n)\) 给定输入,我们可以使用以下分类规则:
并且我们可以使用最大后验概率(MAP)估计来估计 \(P(y)\) 和 \(P(x_i \mid y)\) ;前者则是上课的相对频率 \(y\) 在训练集中。
不同的朴素Bayes分类器的不同主要在于它们对分布所做的假设 \(P(x_i \mid y)\) .
尽管它们的假设显然过于简单,但天真的Bayes分类器在许多现实世界的情况下都表现得很好,例如文档分类和垃圾邮件过滤。它们需要少量的训练数据来估计必要的参数。(For天真的Bayes工作良好的理论原因,以及它对哪些类型的数据有效,请参阅下面的参考文献。)
与更复杂的方法相比,朴素的Bayes学习器和分类器的速度可能非常快。类别条件特征分布的脱钩意味着每个分布都可以独立估计为一维分布。这反过来又有助于缓解维度诅咒带来的问题。
另一方面,尽管朴素Bayes被认为是一个不错的分类器,但众所周知它是一个糟糕的估计器,因此概率输出来自 predict_proba
不要太认真。
引用#
H.张(2004)。 The optimality of Naive Bayes. 程序。flIRS。
1.9.1. 高斯天真的Bayes#
GaussianNB
实现高斯Naive Bayes算法进行分类。假设特征的可能性是高斯的:
的参数 \(\sigma_y\) 和 \(\mu_y\) 使用最大可能性估计。
>>> from sklearn.datasets import load_iris
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.naive_bayes import GaussianNB
>>> X, y = load_iris(return_X_y=True)
>>> X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)
>>> gnb = GaussianNB()
>>> y_pred = gnb.fit(X_train, y_train).predict(X_test)
>>> print("Number of mislabeled points out of a total %d points : %d"
... % (X_test.shape[0], (y_test != y_pred).sum()))
Number of mislabeled points out of a total 75 points : 4
1.9.2. 多项式朴素贝叶斯#
MultinomialNB
implements the naive Bayes algorithm for multinomially
distributed data, and is one of the two classic naive Bayes variants used in
text classification (where the data are typically represented as word vector
counts, although tf-idf vectors are also known to work well in practice).
The distribution is parametrized by vectors
\(\theta_y = (\theta_{y1},\ldots,\theta_{yn})\)
for each class \(y\), where \(n\) is the number of features
(in text classification, the size of the vocabulary)
and \(\theta_{yi}\) is the probability \(P(x_i \mid y)\)
of feature \(i\) appearing in a sample belonging to class \(y\).
的参数 \(\theta_y\) 通过最大似然度的平滑版本估计,即相对频率计数:
哪里 \(N_{yi} = \sum_{x \in T} x_i\) 是功能的次数 \(i\) 出现在班级的所有样本中 \(y\) 训练集中 \(T\) ,而且 \(N_{y} = \sum_{i=1}^{n} N_{yi}\) 是班级所有要素的总数 \(y\) .
The smoothing priors \(\alpha \ge 0\) account for features not present in the learning samples and prevent zero probabilities in further computations. Setting \(\alpha = 1\) is called Laplace smoothing, while \(\alpha < 1\) is called Lidstone smoothing.
1.9.3. 补充天真的Bayes#
ComplementNB
实现了互补朴素贝叶斯(CNB)算法。CNB是标准的多项式朴素贝叶斯(MNB)算法的一种适应,特别适合于不平衡的数据集。具体而言,CNB使用来自 complement 以计算模型的权重。CNB的发明人根据经验表明,CNB的参数估计比MNB的参数估计更稳定。此外,CNB在文本分类任务方面经常优于MNB(通常以相当大的差距)。
权重计算#
计算权重的步骤如下:
where the summations are over all documents \(j\) not in class \(c\), \(d_{ij}\) is either the count or tf-idf value of term \(i\) in document \(j\), \(\alpha_i\) is a smoothing hyperparameter like that found in MNB, and \(\alpha = \sum_{i} \alpha_i\). The second normalization addresses the tendency for longer documents to dominate parameter estimates in MNB. The classification rule is:
即,将文档分配给 poorest 补充匹配。
引用#
雷尼,JD,施,L.,Teevan,J.,& Karger,D. R.(2003)。 Tackling the poor assumptions of naive bayes text classifiers. 在ICML中(第3卷,pp. 616-623)。
1.9.4. 伯努里天真的贝耶斯#
BernoulliNB
为根据多变量伯努利分布的数据实现朴素贝叶斯训练和分类算法;即,可以有多个特征,但是每个特征都被假定为二进制值(伯努利,布尔)变量。因此,该类要求样本表示为二进制值特征向量;如果传递任何其他类型的数据, BernoulliNB
实例可能会二进制化其输入(取决于 binarize
参数)。
伯努里天真Bayes的决策规则基于
它与多项NB规则的不同之处在于,它明确惩罚特征的不出现 \(i\) 这是阶级的一个指标 \(y\) ,其中多项变体将简单地忽略不发生的特征。
在文本分类的情况下,可以使用单词出现载体(而不是单词计数载体)来训练和使用该分类器。 BernoulliNB
可能在某些数据集上表现更好,尤其是那些文档较短的数据集。如果时间允许,建议对这两种模型进行评估。
引用#
C.D.曼宁、P. Raghavan和H. Schütze(2008)。信息检索简介。剑桥大学出版社,2001年. 234-265.
A. McCallum and K. Nigam (1998). A comparison of event models for Naive Bayes text classification. Proc. AAAI/ICML-98 Workshop on Learning for Text Categorization, pp. 41-48.
V.梅西斯,I.安德鲁索普洛斯和G.帕利乌拉斯(2006)。 Spam filtering with Naive Bayes -- Which Naive Bayes? 第三届电子邮件和反垃圾邮件会议(CEAS)。
1.9.5. 绝对天真的Bayes#
CategoricalNB
为类别分布的数据实现类别朴素Bayes算法。它假设指数描述的每个特征 \(i\) ,有自己的类别分布。
针对每个特征 \(i\) 训练集中 \(X\) , CategoricalNB
估计X的每个特征i以类别y为条件的类别分布。样本的索引集定义为 \(J = \{ 1, \dots, m \}\) , \(m\) 作为样本数量。
概率计算#
类别的概率 \(t\) 的粉质 \(i\) 给定类 \(c\) 估计为:
where \(N_{tic} = |\{j \in J \mid x_{ij} = t, y_j = c\}|\) is the number of times category \(t\) appears in the samples \(x_{i}\), which belong to class \(c\), \(N_{c} = |\{ j \in J\mid y_j = c\}|\) is the number of samples with class c, \(\alpha\) is a smoothing parameter and \(n_i\) is the number of available categories of feature \(i\).
CategoricalNB
假设样本矩阵 \(X\) 被编码(例如在 OrdinalEncoder
)这样每个功能的所有类别 \(i\) 用数字表示 \(0, ..., n_i - 1\) 哪里 \(n_i\) 是可用功能类别的数量 \(i\) .
1.9.6. 核心外天真的Bayes模型匹配#
朴素贝叶斯模型可以用来解决大规模的分类问题,对于这些问题,完整的训练集可能不适合内存。为了处理此案, MultinomialNB
, BernoulliNB
,而且 GaussianNB
暴露 partial_fit
可以像使用其他分类器一样增量使用的方法,如中所示 sphx_glr_auto_examples_applications_plot_out_of_core_classification.py .所有天真的Bayes分类器都支持样本加权。
违背 fit
方法,第一次调用 partial_fit
需要传递所有预期类标签的列表。
有关scikit-learn中可用策略的概述,请参阅 out-of-core learning 文献.
备注
的 partial_fit
朴素Bayes模型的方法调用引入了一些计算负担。建议使用尽可能大的数据块大小,即可用RAM允许的范围内。