naiveBayes - juedaiyuer/researchNote GitHub Wiki
#朴素贝叶斯#
##优缺点##
优点:在数据较少的情况下仍然有效,可以处理多类别问题
缺点:对于输入数据的准备方式较为敏感
适用数据类型:标称型数据
##贝叶斯决策##
选择高概率对应的类别
##条件概率##
P(A|B)=P(A and B)/P(B)
贝叶斯准则:p(c|x)=p(x|c)p(c)/p(x)
##使用朴素贝叶斯进行文档分类##
每个特征同等重要
##准备数据:从文本中构建词向量##
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
'''
Task:创建一个包含在所有文档中出现的不重复词的列表
'''
def createVocabList(dataSet):
vocabSet = set([]) #create empty set
for document in dataSet:
vocabSet = vocabSet | set(document) #union of the two sets
return list(vocabSet)
'''
Task:
'''
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
###测试###
>>> import os
>>> os.chdir("/home/juedaiyuer/mycode/researchNote/machinelearning/Ch04")
>>> import sys
>>> sys.path.append("/home/juedaiyuer/mycode/researchNote/machinelearning/Ch04")
RUN起来,没有出现重复的单词
>>> import bayes
>>> listOPosts,listClasses=bayes.loadDataSet()
>>> listOPosts
['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'](/juedaiyuer/researchNote/wiki/'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')
>>> myVocabList=bayes.createVocabList(listOPosts)
>>> myVocabList
['cute', 'love', 'help', 'garbage', 'quit', 'I', 'problems', 'is', 'park', 'stop', 'flea', 'dalmation', 'licks', 'food', 'not', 'him', 'buying', 'posting', 'has', 'worthless', 'ate', 'to', 'maybe', 'please', 'dog', 'how', 'stupid', 'so', 'take', 'mr', 'steak', 'my']
函数setOfWords2Vec()运行效果
>>> listOPosts[0]
['my', 'dog', 'has', 'flea', 'problems', 'help', 'please']
>>> bayes.setOfWords2Vec(myVocabList,listOPosts[0])
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1]
##训练算法:从词向量计算概率##
朴素贝叶斯分类器训练函数
'''
输入参数:
trainMatrix:文档矩阵
二类分类问题,首先计算文档属于侮辱性文档(class=1)的概率,即p(1)
'''
def trainNB0(trainMatrix,trainCategory):
numTrainDocs = len(trainMatrix)
numWords = len(trainMatrix[0])
pAbusive = sum(trainCategory)/float(numTrainDocs)
p0Num = ones(numWords); p1Num = ones(numWords) #change to 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 = log(p1Num/p1Denom) #change to log()
p0Vect = log(p0Num/p0Denom) #change to log()
return p0Vect,p1Vect,pAbusive
###测试###
>>> from numpy import *
>>> reload(bayes)
<module 'bayes' from 'bayes.pyc'>
>>> listOPosts,listClasses=bayes.loadDataSet()
##source##
- 机器学习实战:第4章