码迷,mamicode.com
首页 > 编程语言 > 详细

机器学习 Python实现 贝叶斯算法

时间:2015-03-06 12:47:43      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:正则表达式   垃圾邮件   机器学习   贝叶斯   

[[‘my‘,‘dog‘,‘has‘,‘flea‘,‘problems‘,‘help‘,‘please‘],    0

 [‘maybe‘,‘not‘,‘take‘,‘him‘,‘to‘,‘dog‘,‘park‘,‘stupid‘],  1

 [‘my‘,dalmation,‘is‘,‘so‘,‘cute‘,‘I‘,‘love‘,‘him‘],          0

 [‘stop‘,‘posting‘,‘stupid‘,‘worthless‘,‘garbage‘],          1

 [mr,‘licks‘,‘ate‘,‘my‘,‘steak‘,‘how‘,‘to‘,‘stop‘,‘him‘],  0

[‘quit‘,‘buying‘,‘worthless‘,‘dog‘,‘food‘,‘stupid‘]]           1


以上是六句话,标记是0句子的表示正常句,标记是1句子的表示为粗口。我们通过分析每个句子中的每个词,在粗口句或是正常句出现的概率,可以找出那些词是粗口。

注意:主要从以下两点对分类器进行修改

<1>贝叶斯概率需要计算多个概率的乘积以获得文档属于某个类别的概率,即计算p(w0|1)p(w1|1)p(w2|1)。如果其中一个概率值为0,那么最后的乘积也为0

<2>第二个问题就是下溢出,这是由于太多过小的数相乘造成的。由于大部分因子都非常小,所以程序会下溢出或者得不到正确的答案。解决办法是对乘积取自然对数这样可以避免下溢出或者浮点数舍入导致的错误。

<3>每个单词的出现与否作为一个特征,被称为词集模型;在词袋模型中,每个单词可以出现多次。

贝叶斯理论就是通过可以求得的后验概率计算前验概率
这里用到的朴素贝叶斯理论有两个前提
特征之间相互独立
特征之间同等重要
# -*- coding: cp936 -*-
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 createVocabList(dataSet):  
    vocabSet = set([])      #创建一个空集  
    for document in dataSet:  
        vocabSet = vocabSet | set(document)   #创建两个集合的并集  
    return list(vocabSet)  
  
#将文档词条转换成词向量  
def setOfWords2Vec(vocabList, inputSet):  
    returnVec = [0]*len(vocabList)        #创建一个其中所含元素都为0的向量  
    for word in inputSet:  
        if word in vocabList:  
            #returnVec[vocabList.index(word)] = 1     #index函数在字符串里找到字符第一次出现的位置  词集模型  
            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])   #文本中词汇数量也是词库的大小
    pAbusive = sum(trainCategory)/float(numTrainDocs)  #P(wi)
   # p0Num = zeros(numWords); p1Num = zeros(numWords)  
    #p0Denom = 0.0; p1Denom = 0.0  
    p0Num = ones(numWords); p1Num = ones(numWords)#避免一个概率值为0,最后的乘积也为0  
    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])  
    #p1Vect = p1Num / p1Denom  
    #p0Vect = p0Num / p0Denom   #计算获得p(wi|c1),p(wi|c0)
    #print (p1Num)
    #print (p1Denom)
    p1Vect = log(p1Num / p1Denom)  
    p0Vect = log(p0Num / p0Denom)      #避免下溢出或者浮点数舍入导致的错误   下溢出是由太多很小的数相乘得到的  
    return p0Vect, p1Vect, pAbusive

#朴素贝叶斯分类器  
def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):  #vec2Classify表示需要分类的向量
    p1 = sum(vec2Classify*p1Vec) + log(pClass1)  
    p0 = sum(vec2Classify*p0Vec) + log(1.0-pClass1)  
    if p1 > p0:  
        return 1  
    else: return 0  
  
def test():
    listPosts,listClasses = loadDataSet()
    myVocablist = createVocabList(listPosts)
    set1 = setOfWords2Vec(myVocablist,listPosts[0])
    #print(set1)
    set2 = setOfWords2Vec(myVocablist,listPosts[1])
    #print(set2)
    trainMat = []
    for postinDoc in listPosts:
        trainMat.append(setOfWords2Vec(myVocablist,postinDoc))
    #print(trainMat)
    #print(listClasses)
    p0V,p1V,pAb = trainNB0(trainMat,listClasses)
    print(pAb)
    print(p1V)
    testEntry = ['love','my','dalmation']  
    thisDoc = array(setOfWords2Vec(myVocablist, testEntry))  
    print testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb)  
    testEntry = ['stupid','garbage']  
    thisDoc = array(setOfWords2Vec(myVocablist, testEntry))  
    print testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb)

#垃圾邮件过滤的例子:
def textParse(bigString):      #正则表达式进行文本分割  
    import re  
    listOfTokens = re.split(r'\W*',bigString)  
    return [tok.lower() for tok in listOfTokens if len(tok) > 2]  
  
def spamTest():  
    docList = []; classList = []; fullText = []  
    for i in range(1,26):                          #导入并解析文本文件  
        wordList = textParse(open('E:/python project/Bayes/email/spam/%d.txt' % i).read())  
        docList.append(wordList)  
        fullText.extend(wordList)  
        classList.append(1)  
        wordList = textParse(open('E:/python project/Bayes/email/ham/%d.txt' % i).read())  
        docList.append(wordList)  
        fullText.extend(wordList)  
        classList.append(0)  
    vocabList = createVocabList(docList)  
    trainingSet = range(50);testSet = []  
    for i in range(10):                         #随机构建10个测试集  
        randIndex = int(random.uniform(0,len(trainingSet)))  
        testSet.append(trainingSet[randIndex])  
        del(trainingSet[randIndex])  
    trainMat = []; trainClasses = []  
    for docIndex in trainingSet:  
        trainMat.append(setOfWords2Vec(vocabList, docList[docIndex]))  
        trainClasses.append(classList[docIndex])  
    p0V, p1V, pSpam = trainNB0(array(trainMat), array(trainClasses))  
    errorCount = 0  
    for docIndex in testSet:              #对测试集进行分类  
        wordVector = setOfWords2Vec(vocabList, docList[docIndex])  
        if classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]:  
            errorCount += 1  
    print 'the error rate is: ', float(errorCount)/len(testSet)  



机器学习 Python实现 贝叶斯算法

标签:正则表达式   垃圾邮件   机器学习   贝叶斯   

原文地址:http://blog.csdn.net/xietingcandice/article/details/44096439

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!