提交 6feb010d 编写于 作者: H Hanmin Qin 提交者: loopyme

1.8 交叉分解 1.9 朴素贝叶斯 (0.21.3)

上级 ed150de3
......@@ -16,7 +16,7 @@
![P(x_i | y, x_1, \dots, x_{i-1}, x_{i+1}, \dots, x_n) = P(x_i | y) ,](img/9fd83615429a9be9e5698d35bec8642a.jpg)
对于所有的 :*i* ,这个关系式可以简化为
对于所有的 :*i* 都成立,这个关系式可以简化为
![P(y \mid x_1, \dots, x_n) = \frac{P(y) \prod_{i=1}^{n} P(x_i \mid y)}{P(x_1, \dots, x_n)}](img/1c12ea7ea179efd16ce513645034d41a.jpg)
......@@ -71,17 +71,15 @@ Number of mislabeled points out of a total 150 points : 6
式中![N_{yi} = \sum_{x \in T} x_i](img/bayes01.png)是 训练集T中特征i在类![y](img/0775c03fc710a24df297dedcec515aaf.jpg)中出现的次数,![N_{y} = \sum_{i=1}^{|T|} N_{yi}](img/bayes02.png) 是类 ![y](img/0775c03fc710a24df297dedcec515aaf.jpg) 中出现所有特征的计数总和。
先验平滑因子 ![\alpha \ge 0](img/43fc75930300610d8cb41a8d15899c15.jpg) 应用于在学习样本中没有出现的特征,以防在将来的计算中出现0概率输出。 把 ![\alpha = 1](img/3e0dffeddefe6ba1e809bd9b6276c771.jpg) 被称为拉普拉斯平滑(Lapalce smoothing),而 ![\alpha < 1](img/8fe0d726b364f224c93e31cd3248a1f6.jpg) 被称为Lidstone平滑方法(Lidstone smoothing)。
先验平滑因子 ![\alpha \ge 0](img/43fc75930300610d8cb41a8d15899c15.jpg) 为在学习样本中没有出现的特征而设计,以防在将来的计算中出现0概率输出。 把 ![\alpha = 1](img/3e0dffeddefe6ba1e809bd9b6276c771.jpg) 被称为拉普拉斯平滑(Lapalce smoothing),而 ![\alpha < 1](img/8fe0d726b364f224c93e31cd3248a1f6.jpg) 被称为Lidstone平滑方法(Lidstone smoothing)。
## 1.9.3. 补充朴素贝叶斯
[`ComplementNB`](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.ComplementNB.html#sklearn.naive_bayes.ComplementNB)实现了多项朴素贝叶斯(CNB)算法。CNB是标准多项式朴素贝叶斯(MNB)算法的一种改进,特别适用于不平衡数据集。具体来说,CNB使用来自每个类的补数的统计数据来计算模型的权重。CNB的发明者的经验表明,CNB的参数估计比MNB的参数估计更稳定。此外,CNB在文本分类任务上通常比MNB表现得更好(通常有相当大的优势)。计算权重的步骤如下:
伯努利朴素贝叶斯的决策规则基于
[`ComplementNB`](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.ComplementNB.html#sklearn.naive_bayes.ComplementNB)实现了补充朴素贝叶斯(CNB)算法。CNB是标准多项式朴素贝叶斯(MNB)算法的一种改进,特别适用于不平衡数据集。具体来说,CNB使用来自每个类的补数的统计数据来计算模型的权重。CNB的发明者的研究表明,CNB的参数估计比MNB的参数估计更稳定。此外,CNB在文本分类任务上通常比MNB表现得更好(通常有相当大的优势)。计算权重的步骤如下:
![\begin{align}\begin{aligned}\hat{\theta}_{ci} = \frac{\alpha_i + \sum_{j:y_j \neq c} d_{ij}}{\alpha + \sum_{j:y_j \neq c} \sum_{k} d_{kj}}\\w_{ci} = \log \hat{\theta}_{ci}\\w_{ci} = \frac{w_{ci}}{\sum_{j} |w_{cj}|}\end{aligned}\end{align}](img/bayse03.png)
其中对不在类c中的所有记录j求和,d<sub>ij</sub>既是计数也是记录j中i的tf-idf值,α<sub>i</sub>是就像MNB中一样的平滑超参数,同时![\alpha = \sum_{i} \alpha_i](img/bayse04.png)。第二个归一化解决了长记录主导MNB参数估计的问题。分类规则为:
其中对不在类c中的所有记录j求和,d<sub>ij</sub>可以是文档j中词语i的计数或tf-idf值,α<sub>i</sub>是就像MNB中一样的平滑超参数,同时![\alpha = \sum_{i} \alpha_i](img/bayse04.png)。第二个归一化解决了长记录主导MNB参数估计的问题。分类规则为:
![\hat{c} = \arg\min_c \sum_{i} t_i w_{ci}](img/bayse05.png)
......@@ -97,14 +95,14 @@ Number of mislabeled points out of a total 150 points : 6
与多项分布朴素贝叶斯的规则不同 伯努利朴素贝叶斯明确地惩罚类 ![y](img/0775c03fc710a24df297dedcec515aaf.jpg) 中没有出现作为预测因子的特征 ![i](img/43e13b580daefe5ba754b790dfbd216c.jpg) ,而多项分布分布朴素贝叶斯只是简单地忽略没出现的特征。
在文本分类的例子中,词频向量(word occurrence vectors)(而非词数向量(word count vectors))可能用于训练和用于这个分类器。 `BernoulliNB` 可能在一些数据集上可能表现得更好,特别是那些更短的文档。 如果时间允许,建议对两个模型都进行评估。
在文本分类的例子中,统计词语是否出现的向量(word occurrence vectors)(而非统计词语出现次数的向量(word count vectors))可以用于训练和使用这个分类器。 `BernoulliNB` 可能在一些数据集上表现得更好,特别是那些更短的文档。 如果时间允许,建议对两个模型都进行评估。
> **参考资料**:
>* C.D. Manning, P. Raghavan and H. Schütze (2008). Introduction to Information Retrieval. Cambridge University Press, pp. 234-265.
>* A. McCallum and K. Nigam (1998). [A comparison of event models for Naive Bayes text classification.](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.46.1529) Proc. AAAI/ICML-98 Workshop on Learning for Text Categorization, pp. 41-48.
>* V. Metsis, I. Androutsopoulos and G. Paliouras (2006). [Spam filtering with Naive Bayes – Which Naive Bayes?](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.61.5542) 3rd Conf. on Email and Anti-Spam (CEAS).
## 1.9.5. 堆外朴素贝叶斯模型拟合
## 1.9.5. 基于外存的朴素贝叶斯模型拟合
朴素贝叶斯模型可以解决整个训练集不能导入内存的大规模分类问题。 为了解决这个问题, [`MultinomialNB`](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.MultinomialNB.html#sklearn.naive_bayes.MultinomialNB "sklearn.naive_bayes.MultinomialNB"), [`BernoulliNB`](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.BernoulliNB.html#sklearn.naive_bayes.BernoulliNB "sklearn.naive_bayes.BernoulliNB"), 和 [`GaussianNB`](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html#sklearn.naive_bayes.GaussianNB "sklearn.naive_bayes.GaussianNB") 实现了 `partial_fit` 方法,可以动态的增加数据,使用方法与其他分类器的一样,使用示例见 [Out-of-core classification of text documents](https://scikit-learn.org/stable/auto_examples/applications/plot_out_of_core_classification.html#sphx-glr-auto-examples-applications-plot-out-of-core-classification-py) 。所有的朴素贝叶斯分类器都支持样本权重。
......@@ -112,4 +110,4 @@ Number of mislabeled points out of a total 150 points : 6
对于 scikit-learn 中可用方案的概览,另见 [out-of-core learning](66#71-使用外核学习实例进行拓展) 文档。
> 注意:所有朴素贝叶斯模型调用 `partial_fit` 都会引入一些计算开销。推荐让数据越大越好,其大小与 RAM 中可用内存大小相同。
> 注意:所有朴素贝叶斯模型调用 `partial_fit` 都会引入一些计算开销。推荐让数据越大越好,其大小与 RAM 中可用内存大小相同。
......@@ -42,7 +42,7 @@
>>> from sklearn import svm
>>> X = [[0, 0], [1, 1]]
>>> y = [0, 1]
>>> clf = svm.SVC()
>>> clf = svm.SVC(gamma='scale')
>>> clf.fit(X, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',
......@@ -82,7 +82,7 @@ array([1, 1]...)
```py
>>> X = [[0], [1], [2], [3]]
>>> Y = [0, 1, 2, 3]
>>> clf = svm.SVC(decision_function_shape='ovo')
>>> clf = svm.SVC(gamma='scale', decision_function_shape='ovo')
>>> clf.fit(X, Y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovo', degree=3, gamma='scale', kernel='rbf',
......@@ -185,7 +185,7 @@ LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
>>> y = [0.5, 2.5]
>>> clf = svm.SVR()
>>> clf.fit(X, y)
SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='scale',
SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto_deprecated',
kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)
>>> clf.predict([[1, 1]])
array([ 1.5])
......@@ -297,7 +297,7 @@ array([ 1.5])
>>> gram = np.dot(X, X.T)
>>> clf.fit(gram, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto',
decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
kernel='precomputed', max_iter=-1, probability=False,
random_state=None, shrinking=True, tol=0.001, verbose=False)
>>> # 预测训练样本
......
......@@ -2,6 +2,7 @@
校验者:
        [@peels](https://github.com/apachecn/scikit-learn-doc-zh)
[@qinhanmin2014](https://github.com/qinhanmin2014)
翻译者:
        [@Counting stars](https://github.com/apachecn/scikit-learn-doc-zh)
......@@ -11,7 +12,7 @@
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_compare_cross_decomposition_0011.png](img/88ef3c9a51bdadd21593bf89887a04b5.jpg)](https://scikit-learn.org/stable/auto_examples/cross_decomposition/plot_compare_cross_decomposition.html)
交叉分解算法能够找到两个矩阵 (X 和 Y) 的基础关系。它们是对在两个空间的 协方差结构进行建模的隐变量方法。它们将尝试在X空间中找到多维方向,该方向能 够解释Y空间中最大多维方差方向。PLS回归特别适用于当预测变量矩阵具有比观测值 更多的变量以及当X值存在多重共线性时。相比之下,在这些情况下,标准回归将失败。
交叉分解算法能够找到两个矩阵 (X 和 Y) 的基础关系。它们是对在两个空间的协方差结构进行建模的隐变量方法。它们将尝试在X空间中找到多维方向,该方向能够解释Y空间中最大多维方差方向。 PLS回归特别适用于当预测变量矩阵具有比观测值更多的变量以及当X值存在多重共线性时。相比之下,在这些情况下,标准回归将失败。
包含在此模块中的类有:[`PLSRegression`](https://scikit-learn.org/stable/modules/generated/sklearn.cross_decomposition.PLSRegression.html#sklearn.cross_decomposition.PLSRegression "sklearn.cross_decomposition.PLSRegression"), [`PLSCanonical`](https://scikit-learn.org/stable/modules/generated/sklearn.cross_decomposition.PLSCanonical.html#sklearn.cross_decomposition.PLSCanonical "sklearn.cross_decomposition.PLSCanonical"), [`CCA`](https://scikit-learn.org/stable/modules/generated/sklearn.cross_decomposition.CCA.html#sklearn.cross_decomposition.CCA "sklearn.cross_decomposition.CCA"), [`PLSSVD`](https://scikit-learn.org/stable/modules/generated/sklearn.cross_decomposition.PLSSVD.html#sklearn.cross_decomposition.PLSSVD "sklearn.cross_decomposition.PLSSVD")
> **参考资料**:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册