朴素贝叶斯

今天看了朴素贝叶斯,在网上看到了篇朴素贝叶斯的文章感觉特别好,在此摘抄
算法杂货铺——分类算法之朴素贝叶斯分类(Naive Bayesian classification)

2、机器实战代码,垃圾邮件分类

注:1、

p(ci|w)=p(w|ci)p(ci)p(w)

我们将使用上述公式,对每个计算该值,然后比较这两个概率值大小。
2、利用贝叶斯分类器对文档进行分类时,要计算多个概率的乘积以获得文档属于某个文档类别的概率,即计算 p(w0|1)p(w1|1)p(w2|1) ,如果其中一个概率值为0,那么最后的成绩也为0.为了降低这种影响可以将所有词的出现数初始化为1,并将分母初始化为2
3、另一个遇到的问题是下溢出,这是由于太多很小的数相乘造成的。当计算 p(w0|ci)p(w1|ci)p(w2|ci)p(wn|ci) ,由于大部分因子都很小,所以程序会下溢出或者得不到正确答案
为了解决这个问题于是对乘积取自然对数,以此避免下溢出或者浮点数舍入导致的错误

#encoding:utf-8
import numpy as np
from numpy import *
def loadDataSet():#创建一个实验样本
    postingList=[['my', 'dog', 'has', 'flea', 'problems', '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']]
    classVec = [0,1,0,1,0,1]
    return postingList,classVec
def creatVocabList(dataSet):#创建一个包含所有文档中出现不重复的词表
    vocabSet = set()
    for document in dataSet:
        vocabSet = vocabSet | set(document)#集合的合并
    return list(vocabSet)
def setOfWordsVec(vocabList, inputSet):#输入文档表输出文档向量
    returnVec = [0]*len(vocabList)#创建一个行向量
    for word in inputSet:
        if word in vocabList:#如果在词表中出现,则对应词表中的词中的位置变为1
            returnVec[vocabList.index(word)] = 1
        else:
            print "the word : %s is not in my Vocabulary!" % word
    return returnVec#返回的是行向量
def trainNB0(trainMatrix, trainCategory):#朴素贝叶斯分类器的训练函数
      numTrainDocs = len(trainMatrix)#样本个数(行向量)
      numWords = len(trainMatrix[0])#词汇数目(列向量)
      pAbsive = sum(trainCategory)/float(numTrainDocs)#样本是侮辱性话语的概率
#       p0Num = zeros(numWords)
#       p1Num = zeros(numWords)
#       p0Denom = 0.0;p1Denom = 0.0
      p0Num = ones(numWords)
      p1Num = ones(numWords)
      p0Denom = 2.0;p1Denom = 2.0
      for i in range(numTrainDocs):#遍历所有样本
          if trainCategory[i] == 1:#如果是侮辱性的
              p1Num += trainMatrix[i]#统计各个词汇出现的频数
              p1Denom += sum(trainMatrix[i])#计算词汇总数
          else:
              p0Num += trainMatrix[i]
              p0Denom += sum(trainMatrix[i])
#       p1Vec = p1Num/float(p1Denom)
#       p0Vec = p0Num/float(p0Denom)
      p1Vec = log(p1Num/float(p1Denom))
      p0Vec = log(p0Num/float(p0Denom))
      return p0Vec, p1Vec, pAbsive#返回p(wi|c1),p(wi|c0),p(c1) wi 词汇表里面的各个词汇c1代表侮辱类 c非侮辱类
def classifyNB(vec2Classify, p0Vec, p1Vec, pClass):#分类器
    #vec2Classify 这里起筛选的作用,选出词条的特征
    p1 = sum(vec2Classify*p1Vec)+log(pClass)#得出p(w0|1)p(w1|1)p(w2|1)p(1)
    p0 = sum(vec2Classify*p0Vec)+log(1.0-pClass)#得出p(w0|0)p(w1|0)p(w2|0)p(0)
    if p1 > p0:
        return 1
    else:
        return 0
def testingNB():   #测试函数                 
    listOPosts,listClasses = loadDataSet()
    myVocabList = creatVocabList(listOPosts)
    trainMat = []
    for i in range(len(listOPosts)):
       trainMat.append(setOfWordsVec(myVocabList, listOPosts[i]))
    p0V,p1V,pAb = trainNB0(trainMat, listClasses)
    test = ['love', 'my', 'dalmation']
    thisDoc = array(setOfWordsVec(myVocabList, test))
    print classifyNB(thisDoc, p0V,p1V,pAb)
    test = ['stupid', 'garbage']
    thisDoc = array(setOfWordsVec(myVocabList, test))
    print classifyNB(thisDoc, p0V,p1V,pAb)
def textParse(bigString):#文本解析
    import re 
    listOfTokens = re.split(r'\W*', bigString)#正则表达  剔除非英文非数字   
#     print listOfTokens
    return [tok.lower() for tok in listOfTokens if len(tok) > 2]#只要2个字母以上的单词
def spamTest():#测试函数
    docList=[];classList=[];fullText=[]
    for i in range(1,26):#根据邮件类型建立词表
        wordList =textParse(open(r'spam/%d.txt' % i).read())#正常邮件
        docList.append(wordList)       
        fullText.append(wordList)
        classList.append(1)        
        wordList =textParse(open(r'ham/%d.txt' % i).read())#垃圾邮件
        docList.append(wordList)
        fullText.append(wordList)
        classList.append(0)
    vocabList = creatVocabList(docList)#建立词汇表
    print "-------------"

    trainSet=range(50);testSet=[]
    for i in range(10):
        randIndex = int(random.uniform(0, len(trainSet)))
        testSet.append(trainSet[randIndex])
        del trainSet[randIndex]

    trainMat=[];trainClass=[]
    for docIndex in trainSet:
        trainMat.append(setOfWordsVec(vocabList, docList[docIndex]))
        trainClass.append(classList[docIndex])
    p0V,p1V,pSpam = trainNB0(trainMat, trainClass)#训练
    errorCount = 0
    for docIndex in testSet:#计算错误率
        wordVector = setOfWordsVec(vocabList, docList[docIndex])
        if classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]:
            errorCount+=1
    print  "the error rate is:",float(errorCount)/len(testSet)    


i = 1       
# wordList =textParse(open(r'spam/%d.txt' % i).read()) 
# print wordList       
spamTest()   
# testingNB()







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值