机器学习实战之朴素贝叶斯算法

朴素贝叶斯采用了属性条件独立性假设,对已知类别,假设所有属性相互独立。

img

d为属性数目,xi为x在第i个属性的取值,因为对所有类别来说P(x)相同,所有只计算分子即可。

详细解释见西瓜书P150

下面说一下机器学习实战中用朴素贝叶斯算法进行文本分类。

文本训练模型算法函数(trainNB0)的步骤如下:

  • 先将文本构建成向量

  • 计算每个类别中的文档数目

  • 对每篇训练文档

    ​ 对每个类别:

    ​ 如果词条出现在文档中>>>增加该词条的计数值

    ​ 增加所有词条的计数值

  • 最后将该词条的数目除以总词条数目得到条件概率

  • 返回每个类别的条件概率

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# bayes.py
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] # 1 is abusive, 0 not
return postingList, classVec

# 创建一个不重复的
def createVocabList(dataSet):
vocabSet = set([]) # create empty set
for document in dataSet:
vocabSet = vocabSet | set(document) # union of the two sets
return list(vocabSet)

# 从文本中构建词向量
def setOfWords2Vec(vocabList, inputSet):
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[0])
pAbusive = sum(trainCategory) / float(numTrainDocs) # P(c)的概率
p0Num = ones(numWords) # 用1初始化,是因为防止某一概率为0造成相乘结果为0.
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])
p1Vect = log(p1Num / p1Denom) # 分类为1的每个词条的数目除以总词条数目得到条件概率
p0Vect = log(p0Num / p0Denom) # 分类为0的每个词条的数目除以总词条数目得到条件概率
return p0Vect, p1Vect, pAbusive

# 对样本进行预测分类
def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):
# 用对数是因为ln(f(x))和f(x)的曲线递增性一致,并且可以防止数太小相乘造成下溢。
# ln(a+b) = lna + lnb 实际上计算的是所有概率相乘去对数。然后比较两个类的值,值大的即为预测的那一类。
p1 = sum(vec2Classify * p1Vec) + log(pClass1)
p0 = sum(vec2Classify * p0Vec) + log(1 - 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

if __name__ == '__main__':
# listOPosts, listClases = loadDataSet()
# myVocabList = createVocabList(listOPosts)
# # print(myVocabList)
# # print(setOfWords2Vec(myVocabList, listOPosts[0]))
# trainMat = []
# for postinDoc in listOPosts:
# trainMat.append(setOfWords2Vec(myVocabList, postinDoc))
# p0V, p1V, pAb = trainNB0(trainMat, listClases)
testingNB()
使用朴素贝叶斯过滤垃圾邮件

使用书上带的数据集进行训练,首先对邮件进行提取词然后构建向量,之后使用朴素贝叶斯算法进行训练。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# email_train.py
from numpy import *

import bayes
import random

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,23):
wordList = textParse(open('G:\机器学习\机器学习实战\机器学习实战(中文版+英文版+源代码)\machinelearninginaction\Ch04\email\spam\\%d.txt' %i).read())
docList.append(wordList)
fullText.extend(wordList)
classList.append(1)
wordList = textParse(open('G:\机器学习\机器学习实战\机器学习实战(中文版+英文版+源代码)\machinelearninginaction\Ch04\email\ham\\%d.txt' %i).read())
docList.append(wordList)
fullText.extend(wordList)
classList.append(0)
vocabList = bayes.createVocabList(docList)
traingsSet = list(range(44)) # 构建训练集和测试集
testSet = []
for i in range(8):
randIndex = int(random.uniform(0,len(traingsSet)))
testSet.append(traingsSet[randIndex])
del(traingsSet[randIndex])
trainMat = []
trainClasses = []
for docIndex in traingsSet:
trainMat.append(bayes.setOfWords2Vec(vocabList,docList[docIndex]))
trainClasses.append( [docIndex])
p0V,p1V,pSpam = bayes.trainNB0(array(trainMat),array(trainClasses))
errorCount = 0 # 输出分类错误单词和错误率
for docIndex in testSet:
wordVector = bayes.bagOfWords2VecMN(vocabList, docList[docIndex])
if bayes.classifyNB(array(wordVector),p0V,p1V,pSpam) != classList[docIndex]:
errorCount += 1
print("classification error",docList[docIndex])
print('the error rate is: ',float(errorCount)/len(testSet))

if __name__ == '__main__':
spamTest()
# emailText = open('G:\机器学习\机器学习实战\机器学习实战(中文版+英文版+源代码)\machinelearninginaction\Ch04\email\spam\\6.txt')
# fr = emailText.read()
#
# print(textParse(fr))
-------------The End-------------