朴素贝叶斯分类(Naive Bayesian classification)及其python实现

来源:互联网 发布:流程图用什么软件画 编辑:程序博客网 时间:2024/06/14 08:52

朴素贝叶斯分类(Naive Bayesian classification)


贝叶斯分类是一类分类算法的总称,这类算法都以贝叶斯定理为基础,所以叫贝叶斯分类。而朴素贝叶斯模型(naive Bayes)是基于贝叶斯定理与特征条件独立假设的分类方法。朴素贝叶斯常常用于文本分类,然而该方法对于英文等语言很好用,对中文就不是很好。

一. 朴素贝叶斯分类算法的优缺点以及应用场景:

1. 优点:在数据较少的情况下仍然有效,可以处理多类别问题;

2. 缺点:对于输入数据的准备方式比较敏感、由于朴素贝叶斯的“朴素”特点,所以会带来一些准确率上的损失、需要计算先验概率,分类决策存在错误率、朴素贝叶斯有分布独立的假设前提,而现实生活中这些很难是完全独立的;

3. 应用场景:文本分类、垃圾文本过滤、情感预测、多分类预测、推荐系统

二.朴素贝叶斯算法

1.条件概率公式:
2.全概率公式:
3.贝叶斯公式:
4.朴素贝叶斯分类原理
    我们有一个已经分类好的样本集X,和一个待分类项,其中每个a是每个样本的特征属性。已知所有样本有n个类别,即

(1)我们想要知道该待分类项x的所属类别,可以求出在待分类项特征为

的条件下的每个类别的概率:

因为分母对于所有类别为常数,因为我们只要将分子最大化为1。其中分子第二项为总样本中类别i出现的概率。所以

分子项为

(2)统计得到各个类别下每个特征属性的条件概率

(3)利用上述得到的条件概率计算得到

(4)如果我们得到,则该待分类项x属于类别yk。

三.使用朴素贝叶斯时的注意点

1. 对测试数据中的0频次项,一定要记得平滑,简单一点可以对每个类别的每一个特征属性相初始化。
2.先处理处理特征,把相关特征去掉,因为高相关度的2个特征在模型中相当于发挥了2次作用。

四.朴素贝叶斯分类python实现

from numpy import *def loadDataSet():    postingList=[['my','dog','has','flea','problem','help','please'],\                 ['maybe','not','take','him','to','dog','park','stupid'],\                 ['my','dalmation','is','so','cute','i','love','him'],\                 ['stop','posting','stupid','worthless','garbage'],\                 ['mr','licks','ate','my','steak','how','to','stop','him'],\                 ['quit','buying','worthless','dog','food','stupid']]    classifyLabel=[0,1,0,1,0,1]    return postingList,classifyLabel#将样本中出现的单词组成一个集合def createVocabList(postingList): #输入样本词条切好后的文档集合        vocabSet=set([])    for document in postingList:        vocabSet=vocabSet|set(document) #创建两个集合的并集        vocabList=list(vocabSet)    return vocabList#求出输入inputSet在样本词汇表中出现的次数def showWordTimes(labelYData,dataXi):    if dataXi in labelYData:        returnTimeVec=1    else:        returnTimeVec=0    return returnTimeVec#计算P(yi),即各个类别在总样本中出现的概率def computeProbability(postingList,classifyLabel):    i=0    j=0    m=len(classifyLabel)    for label in classifyLabel:        if label==0:            i+=1        else:            j+=1    pClassifyLabel0=i/m    pClassifyLabel1=j/m    return pClassifyLabel0,pClassifyLabel1,i,j #返回P(0),P(1),以及样本中类别一和类别二的总数i和j#计算带判断的词条在各个类别下出现的次数def trainNavieBayes(postingList,classifyLabel,testDataSet):    px0=[]    px1=[]    for word in testDataSet:        t0=0.4        t1=0.4        for i in range(len(postingList)):            if classifyLabel[i]==0:                t0=t0+showWordTimes(postingList[i],word)            else:                t1=t1+showWordTimes(postingList[i],word)            px0.append(t0)            px1.append(t1)    return px0,px1 #将各个类别中所有条件概率相乘def sumPx01(pClassifyList):    num=1    for i in pClassifyList:        num*=i    return numpostingList,classifyLabel=loadDataSet()pClassifyLabel0,pClassifyLabel1,i,j=computeProbability(postingList,classifyLabel)testDataSet=['maybe','not','take','he','to','dog','park','stupid']px0,px1=trainNavieBayes(postingList,classifyLabel,testDataSet)pClassify0=(mat(px0)/i).tolist()[0]pClassify1=(mat(px1)/j).tolist()[0]pY0=pClassifyLabel0*sumPx01(pClassify0)pY1=pClassifyLabel1*sumPx01(pClassify1)if pY0>pY1:    print(0)else:    print(1) 




阅读全文
0 0
原创粉丝点击