赞
踩
朴素贝叶斯的实施 基于一种假设:所有特征都是相对独立的,互不相关。这种假设同时也减轻了the curse of dimentionality的影响,具体公式如下:
在sklearn中,不同的朴素贝叶斯classifier的不同之处在于,他们对于P(x|y) distribution的假设不同。
尽管朴素贝叶斯是一个好的分类器,但是,他是一个坏的estimator,因此sklearn中朴素贝叶斯的predict_proba is not taken too seriously。
#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,公式如下:
#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)公式如下:
Nyi:为class y中样本xi的出现次数(word xi);
Ny:为class y中所有特征数;(class y指某一篇文章);
alpha:是平滑系数;用于防止p(x|y)中分母为0的情况。
sklearn.naive_bayes.ComplementNB(alpha=1.0, fit_prior=True, class_prior=None, norm=False)
#alpha:利用training data拟合权重的时候,用来smoothing parameter的系数(laplace/lidstone)。具体用法:当某一word在training data中并没有出现时,如果不加alpha,则p(word|y)=0,那么在计算整个p(x1,x2,...|y)的概率时,将得到0,为避免这一情况的发生,我们在用training data 计算p(x|y)时分子,分母同时加入一个系数,以防止p(x|y)=0的情况发生。
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。
NB可以用于处理一些大规模的分类问题(trainingdata无法全部存入memory中),为处理这些问题,可以利用“ MultinomialNB, BernoulliNB, and GaussianNB”中给出的partial_fit方法。利用partial_fit方法,当training data中传入一个new sample时,model可以根据new sample来调整其自身参数。是一种online machine learning,他解决了无法将全部training data存入memory的困境。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。