一、原理
1 贝叶斯定理
朴素贝叶斯是贝叶斯决策理论的一部分。是贝叶斯最基本、最原始的理论方法。
贝叶斯决策理论中最重要的定理是贝叶斯定理
P
(
A
∣
B
)
=
P
(
B
∣
A
)
P
(
A
)
P
(
B
)
\ P(A|B)= \frac{P(B|A)P(A)}{P(B)}
P(A∣B)=P(B)P(B∣A)P(A)
式中,
P
(
A
)
是
A
的
先
验
概
率
;
P(A)是A的先验概率;
P(A)是A的先验概率;
P
(
A
∣
B
)
是
已
知
B
发
生
后
A
发
生
的
条
件
概
率
,
被
称
为
A
的
后
验
概
率
;
P(A|B)是已知B发生后A发生的条件概率,被称为A的后验概率;
P(A∣B)是已知B发生后A发生的条件概率,被称为A的后验概率;
P
(
B
∣
A
)
是
已
知
A
发
生
后
B
发
生
的
条
件
概
率
,
被
称
为
B
的
后
验
概
率
;
P(B|A)是已知A发生后B发生的条件概率,被称为B的后验概率;
P(B∣A)是已知A发生后B发生的条件概率,被称为B的后验概率;
P
(
B
)
是
B
的
先
验
概
率
,
被
称
为
标
准
化
常
量
。
P(B)是B的先验概率,被称为标准化常量。
P(B)是B的先验概率,被称为标准化常量。
2 使用条件概率来分类
设
A
=
(
a
1
,
.
.
.
,
a
n
)
′
设A=(a_1,...,a_n)'
设A=(a1,...,an)′
应
用
贝
叶
斯
定
理
:
P
(
a
i
∣
B
)
=
P
(
B
∣
a
i
)
P
(
a
i
)
P
(
B
)
应用贝叶斯定理: P(a_i|B)= \frac{P(B|a_i)P(a_i)}{P(B)}
应用贝叶斯定理:P(ai∣B)=P(B)P(B∣ai)P(ai)
1
)
如
果
P
(
a
1
∣
B
)
>
P
(
a
2
∣
B
)
,
就
属
于
类
别
a
1
;
1)如果P(a_1|B)>P(a_2|B),就属于类别a_1;
1)如果P(a1∣B)>P(a2∣B),就属于类别a1;
2
)
如
果
P
(
a
2
∣
B
)
>
P
(
a
1
∣
B
)
,
就
属
于
类
别
a
2
;
2)如果P(a_2|B)>P(a_1|B),就属于类别a_2;
2)如果P(a2∣B)>P(a1∣B),就属于类别a2;
3 使用朴素贝叶斯进行文档分类
3.1 准备数据:从文本中构建词向量
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
3.2 训练算法:利用词向量计算概率
def trainNB0(trainMatrix, trainCategory):
numTrainDocs = len(trainMatrix)
numWords = len(trainMatrix[0])
pAbusive = sum(trainCategory)/float(numTrainDocs)
p0Num = np.ones(numWords); p1Num = np.ones(numWords) #change to np.ones()
p0Denom = 2.0; p1Denom = 2.0 #change to 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 = np.log(p1Num/p1Denom) #change to np.log()
p0Vect = np.log(p0Num/p0Denom) #change to np.log()
return p0Vect, p1Vect, pAbusive
3.3 测试算法
def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):
p1 = sum(vec2Classify * p1Vec) + np.log(pClass1) #element-wise mult
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(setOfWords2Vec(myVocabList, postinDoc))
p0V, p1V, pAb = trainNB0(np.array(trainMat), np.array(listClasses))
testEntry = ['love', 'my', 'dalmation']
thisDoc = np.array(setOfWords2Vec(myVocabList, testEntry))
print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb))
testEntry = ['stupid', 'garbage']
thisDoc = np.array(setOfWords2Vec(myVocabList, testEntry))
print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb))
3.4 词袋模型
def bagOfWords2VecMN(vocabList, inputSet):
returnVec = [0]*len(vocabList)
for word in inputSet:
if word in vocabList:
returnVec[vocabList.index(word)] += 1
return returnVec