朴素贝叶斯的实施 基于一种假设:所有特征都是相对独立的,互不相关。这种假设同时也减轻了the curse of dimentionality的影响,具体公式如下:
(sklearn十九):Naive Bayes-LMLPHP
在sklearn中,不同的朴素贝叶斯classifier的不同之处在于,他们对于P(x|y) distribution的假设不同。
尽管朴素贝叶斯是一个好的分类器,但是,他是一个坏的estimator,因此sklearn中朴素贝叶斯的predict_proba is not taken too seriously。

几种贝叶斯模型

Guassian Naive Bayes

#Guassian NB Can perform online updates to model parameters via partial_fit method.
sklearn.naive_bayes.GaussianNB(priors=None, var_smoothing=1e-09)
#priors:各个类别的先验概率。
#var_smoothing:Portion of the largest variance of all features that is added to variances for calculation stability.???不太明白具体工作原理:是指用最大variance来估计模型稳定性?还是用最大模型来修正模型的稳定性?

在该模型中,p(x|y)服从Gaussian distribution。各个特征的似然值假设为Guassian,公式如下:
(sklearn十九):Naive Bayes-LMLPHP

Multinomial Naive Bayes

#The multinomial Naive Bayes classifier is suitable for classification with discrete features。The multinomial distribution normally requires integer feature counts. However, in practice, fractional counts such as tf-idf may also work.

sklearn.naive_bayes.MultinomialNB(alpha=1.0, fit_prior=True, class_prior=None)
#alpha:Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
#fit_prior:是否学习prior class probability,如果False,则假设prior class probability为uniform distribution
#class_prior:各个类别的先验概率。
#有点儿疑惑:fit_prior和class_prior是否指的都是X distribution,还是fit_prior指的是y distribution,class_prior指的是X distribution。

tf-idf
在Multinominal NB中,X服从多项式分布,该NB分类器常被用于“文本分类”中。在Multinominal NB中,X的先验概率p(x|y)公式如下:
(sklearn十九):Naive Bayes-LMLPHP
Nyi:为class y中样本xi的出现次数(word xi);
Ny:为class y中所有特征数;(class y指某一篇文章);
alpha:是平滑系数;用于防止p(x|y)中分母为0的情况。

Complement Naive Bayes

  • CNB是在MNB的基础上,做了一定的修正,CNB的inventor证明,CNB的参数稳定性要优于MNB,且CNB outperforms
    MNB.
  • Complement NB适用于imbalanced data,其核心思想是:计算the word of dictionary属于每个class的权重(该权重反应了word在每个class中的重要程度,weight of word in class1越大,说明其在class1中的重要性越低,对class1特性的诠释越少)。目标函数是:对于某个test document,求使得sum(ti * wci)最小的那个类别c。(ti:为test document中各个word出现的次数;wci:为各个类别下某一word i的权重)。权重的计算公式,及目标函数具体如下:
    (sklearn十九):Naive Bayes-LMLPHP

Bernoulli Naive Bayes

  • 在BNB中,X服从伯努利分布,一个document的每个word(feature)表示为word occurrence vector(该vector中代表word的维度,值要么为1要么为0),而在MNB中每个word(feature)表示为word count vector(该vector中代表word的维度,值要么为count要么为0)。
  • 在MNB中,如果dictionary中某一word在test document中没有出现,则MNB将对其忽略不计,而在BNB中,将给该word一个惩罚项,即:p(x|y)=1-p(i|y);
  • 这里,p(i|y)是根据trainingdata计算得到的,p(i|y)相当于每个word(特征)i在特定class中出现的频率(计算公式:word i在class中出现的次数 除以 class中所有word的总数)。
    BNB中test document p(x|y)的计算公式如下:
    (sklearn十九):Naive Bayes-LMLPHP
    其中,xi的值为1。
    BernoulliNB might perform better on some datasets, especially those with shorter documents. It is advisable to evaluate both models(BNB,MNB), if time permits.
sklearn.naive_bayes.BernoulliNB(alpha=1.0, binarize=0.0, fit_prior=True, class_prior=None)
#alpha:在利用trainingdata估计p(x|y)时的smoothing parameter(Laplace/Lidstone)。
#binarize:当data feature为连续值时,将各个sample的feature转化为{0,1}的threshold。

Out-of-core naive Bayes model fitting

NB可以用于处理一些大规模的分类问题(trainingdata无法全部存入memory中),为处理这些问题,可以利用“ MultinomialNB, BernoulliNB, and GaussianNB”中给出的partial_fit方法。利用partial_fit方法,当training data中传入一个new sample时,model可以根据new sample来调整其自身参数。是一种online machine learning,他解决了无法将全部training data存入memory的困境。

官方文档:Naive Bayes

10-06 11:33