1. 程式人生 > >python中樸素貝葉斯程式碼的實現

python中樸素貝葉斯程式碼的實現

程式碼主要參考機器學習實戰那本書,發現最近老外的書確實比中國人寫的好,由淺入深,程式碼通俗易懂,不多說上程式碼:

#encoding:utf-8
'''
Created on 2015年9月6日

@author: ZHOUMEIXU204
樸素貝葉斯實現過程
'''


#在該演算法中類標籤為1和0,如果是多標籤稍微改動程式碼既可
import numpy as np
path=u"D:\\Users\\zhoumeixu204\Desktop\\python語言機器學習\\機器學習實戰程式碼   python\\機器學習實戰程式碼\\machinelearninginaction\\Ch04\\"
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]    #1 is abusive, 0 not
    return postingList,classVec
def  createVocabList(dataset):
    vocabSet=set([])
    for  document  in dataset:
        vocabSet=vocabSet|set(document)
    return list(vocabSet)
def  setOfWordseVec(vocabList,inputSet):
    returnVec=[0]*len(vocabList)
    for word  in inputSet:
        if word in vocabList:
            returnVec[vocabList.index(word)]=1    #vocabList.index()  函式獲取vocabList列表某個元素的位置,這段程式碼得到一個只包含0和1的列表
        else:
            print("the word :%s is not in my  Vocabulary!"%word)
    return returnVec
    
listOPosts,listClasses=loadDataSet()
myVocabList=createVocabList(listOPosts)
print(len(myVocabList))
print(myVocabList)
print(setOfWordseVec(myVocabList, listOPosts[0]))
print(setOfWordseVec(myVocabList, listOPosts[3]))
#上述程式碼是將文字轉化為向量的形式,如果出現則在向量中為1,若不出現 ,則為0


def  trainNB0(trainMatrix,trainCategory):   #建立樸素貝葉斯分類器函式
    numTrainDocs=len(trainMatrix)
    numWords=len(trainMatrix[0])
    pAbusive=sum(trainCategory)/float(numTrainDocs)
    p0Num=np.ones(numWords);p1Num=np.ones(numWords)
    p0Deom=2.0;p1Deom=2.0
    for  i  in range(numTrainDocs):
        if trainCategory[i]==1:
            p1Num+=trainMatrix[i]
            p1Deom+=sum(trainMatrix[i])
        else:
            p0Num+=trainMatrix[i]
            p0Deom+=sum(trainMatrix[i])
    p1vect=np.log(p1Num/p1Deom)    #change  to log
    p0vect=np.log(p0Num/p0Deom)    #change to log
    return  p0vect,p1vect,pAbusive

listOPosts,listClasses=loadDataSet()
myVocabList=createVocabList(listOPosts)
trainMat=[]
for  postinDoc  in listOPosts:
    trainMat.append(setOfWordseVec(myVocabList, postinDoc))

p0V,p1V,pAb=trainNB0(trainMat, listClasses)
if __name__!='__main__':
    print("p0的概況")
    print (p0V)
    print("p1的概率")
    print (p1V)
    print("pAb的概率")
    print (pAb)



#構建樣本分類器testEntry=['love','my','dalmation']  testEntry=['stupid','garbage']到底屬於哪個類別
def   classifyNB(vec2Classify,p0Vec,p1Vec,pClass1):
    p1=sum(vec2Classify*p1Vec)+np.log(pClass1)
    p0=sum(vec2Classify*p0Vec)+np.log(1.0-pClass1)
    if p1>p0:
        return  1
    else:
        return 0
def  testingNB():
    listOPosts,listClasses=loadDataSet()
    myVocabList=createVocabList(listOPosts)
    trainMat=[]
    for postinDoc  in listOPosts:
        trainMat.append(setOfWordseVec(myVocabList, postinDoc))
    p0V,p1V,pAb=trainNB0(np.array(trainMat),np.array(listClasses))
    print("p0V={0}".format(p0V))
    print("p1V={0}".format(p1V))
    print("pAb={0}".format(pAb))
    testEntry=['love','my','dalmation']
    thisDoc=np.array(setOfWordseVec(myVocabList, testEntry))
    print(thisDoc)
    print("vec2Classify*p0Vec={0}".format(thisDoc*p0V))
    print(testEntry,'classified as :',classifyNB(thisDoc, p0V, p1V, pAb))
    testEntry=['stupid','garbage']
    thisDoc=np.array(setOfWordseVec(myVocabList, testEntry))
    print(thisDoc)
    print(testEntry,'classified as :',classifyNB(thisDoc, p0V, p1V, pAb))
    
if __name__=='__main__':
    testingNB()  
    
    
#使用樸素貝葉斯過濾垃圾郵件
# 1.收集資料:提供文字檔案
# 2.準備資料:講文字檔案見習成詞條向量
# 3.分析資料:檢查詞條確保解析的正確性
# 4.訓練演算法:使用我們之前簡歷的trainNB0()函式
# 5.測試演算法:使用classifyNB(),並且對建一個新的測試函式來計算文件集的錯誤率
# 6.使用演算法,構建一個完整的程式對一組文件進行分類,將錯分的文件輸出到螢幕上   
# import re
# mySent='this book  is  the best book  on python  or M.L.  I hvae  ever laid eyes upon.'
# print(mySent.split())
# regEx=re.compile('\\W*')
# print(regEx.split(mySent))
# emailText=open(path+"email\\ham\\6.txt").read()

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(path+"email\\spam\\%d.txt"%i).read())
        docList.append(wordList)
        fullText.extend(wordList)
        classList.append(1)
        wordList=textParse(open(path+"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):
        randIndex=int(np.random.uniform(0,len(trainingSet)))
        testSet.append(trainingSet[randIndex])
        del (trainingSet[randIndex])
    trainMat=[];trainClasses=[]
    for   docIndex in trainingSet:
        trainMat.append(setOfWordseVec(vocabList, docList[docIndex]))
        trainClasses.append(classList[docIndex])
    p0V,p1V,pSpam=trainNB0(np.array(trainMat),np.array(trainClasses))
    errorCount=0
    for   docIndex  in testSet:
        wordVector=setOfWordseVec(vocabList, docList[docIndex])
        if  classifyNB(np.array(wordVector), p0V, p1V, pSpam)!=classList[docIndex]:
            errorCount+=1
    print 'the error rate is :',float(errorCount)/len(testSet)
if  __name__=='__main__':
    spamTest()