贝叶斯分类python代码_机器学习实战之朴素贝叶斯进行文档分类(Python 代码版)...
貝葉斯是搞概率論的。學(xué)術(shù)圈上有個(gè)貝葉斯學(xué)派。看起來(lái)吊吊的。關(guān)于貝葉斯是個(gè)啥網(wǎng)上有很多資料。想必讀者基本都明了。我這里只簡(jiǎn)單概括下:貝葉斯分類其實(shí)就是基于先驗(yàn)概率的基礎(chǔ)上的一種分類法,核心公式就是條件概率。舉個(gè)俗氣的例子,通過(guò)我們的以往觀察,鯉魚中尾巴是紅色的占比達(dá)90%,鯽魚中尾巴是紅色的占比只有1%不到,那么新來(lái)了一條小魚,他是鯉魚還是鯽魚呢?我看一下他的尾巴,發(fā)現(xiàn)是紅色,根據(jù)過(guò)去的先驗(yàn)概率經(jīng)驗(yàn),它是鯉魚的概率比較大,我認(rèn)為它是鯉魚。
這當(dāng)時(shí)是個(gè)最簡(jiǎn)單的例子,實(shí)踐中的問題就復(fù)雜了。比如說(shuō)特征不止是尾巴紅不紅,還有魚嘴巴大不大,魚肥不肥,魚身子長(zhǎng)還是寬,各種,而且不是一個(gè)特征就能分辨出來(lái)的,還需要多方分析,然后貝爺感覺這個(gè)那個(gè)的真麻煩,就先假定每個(gè)特征都是獨(dú)立的,如果一條魚紅尾巴大嘴巴肥得很還是長(zhǎng)身子,就這樣求她是鯉魚的概率:鯉魚中紅尾巴0.9*鯉魚中大嘴巴0.3*鯉魚中肥豬0.6*鯉魚中長(zhǎng)身子0.4=0.27*0.24.。。。。
閑話少扯。上代碼分析。我代碼干的不是魚的分類了,而是一篇文檔。
from numpy import *
def loadDataSet():#這個(gè)函數(shù)呢,他建立了一個(gè)敏感詞典,并打了標(biāo)簽,共6個(gè)詞集合,其中2、4、6詞集合中的詞是敏感詞
postingList = [['my','dog','has','flea',\
'problems','help','please'],
['maybe','not','take','him',\
'to','dog','park','stupid'],
['my','dalmation','is','so','cute',\
'T','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):#這個(gè)函數(shù)呢,它是把輸入的dataset(就是一個(gè)新文檔嘛)進(jìn)行分解處理,返回的是這個(gè)文檔沒有重復(fù)詞的列表
vocabSet = set([])
for document in dataSet:
vocabSet = vocabSet | set(document)
return list(vocabSet)
def setOfWords2Vec(vocabList,inputSet):#這個(gè)函數(shù)呢,他就是根據(jù)輸入的新文檔,和詞匯表,來(lái)對(duì)新文檔打標(biāo)簽,看他有多少敏感詞,只要是出現(xiàn)了詞匯表里的詞,就將標(biāo)簽打1,沒有就默認(rèn)為0
returnVec = [0]*len(vocabList)
for word in inputSet:
if word in vocabList:
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)
pAbusive = sum(trainCategory) / float(numTrainDocs)
p0Num = zeros(numWords)
p1Num= zeros(numWords)
p0Denom = 0.0;p1Denom = 0.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
return p0Vect,p1Vect,pAbusive
def classifyNB(vec2Classify,p0Vec,p1Vec,pClass1):
p1= sum(vec2Classify * p1Vec) + log(pClass1)
p0 = sum(vec2Classify * p0Vec) + 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(setOfWords2Vec(myVocabList,postinDoc))
p0V,p1V,pAb = trainNB0(array(trainMat),array(listClasses))
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 bagOfWords2VecMN(vocabList,inputSet):
returnVec = [0]*len(vocabList)
for word in inputSet:
if word in vocabList:
returnVec[vocabList.index(word)] +=1
return returnVec
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:/數(shù)據(jù)挖掘/MLiA_SourceCode/machinelearninginaction/Ch04/email/spam/%d.txt' % i).read())
docList.append(wordList)
fullText.extend(wordList)
classList.append(1)
#?? print('zhe li de i shi %d,',? i)
wordList = textParse(open('E:/數(shù)據(jù)挖掘/MLiA_SourceCode/machinelearninginaction/Ch04/email/ham/%d.txt' % i).read())
docList.append(wordList)
fullText.extend(wordList)
classList.append(0)
vocabList = createVocabList(docList)
trainingSet = list(range(50));testSet=[]
for i in range(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))
總結(jié)
以上是生活随笔為你收集整理的贝叶斯分类python代码_机器学习实战之朴素贝叶斯进行文档分类(Python 代码版)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【翻译】各种Payload免杀工具集
- 下一篇: python汉语叫什么意思_Python