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
#生成的list是二位数组,访问时候应该加list[0],list[1],list[2]
def createVocabList(dataSet):
vocabSet = set([]) #create empty set,set集合中不会重复
for document in dataSet:
vocabSet = vocabSet | set(document) #union of the two sets
# | 求两者的并集
return list(vocabSet) #list去除重复词
def setOfWords2Vec(vocabList, inputSet):
#前者是词汇表,后者是输入的文档
returnVec = [0]*len(vocabList) #生成len长度的0的集合
for word in inputSet:
if word in vocabList:
returnVec[vocabList.index(word)] = 1 #文本数量+1
else: print("the word: %s is not in my Vocabulary!" % word)
return returnVec
def bagOfWords2VecMN(vocabList, inputSet):
returnVec = [0]*len(vocabList)
for word in inputSet:
if word in vocabList:
returnVec[vocabList.index(word)] += 1 #文本数量+1,频率增大
return returnVec
def trainNB0(trainMatrix,trainCategory):#导入文档矩阵和文档标签
numTrainDocs = len(trainMatrix) #文档是二维矩阵,不同文档/行,文档中不同词汇/列
numWords = len(trainMatrix[0])
pAbusive = sum(trainCategory)/float(numTrainDocs)#标签计算,带有侮辱性词汇+1,除以文档数行,带侮辱的概率
p0Num = ones(numWords); p1Num = ones(numWords) #change to ones() ,列/词数置为全1向量,防止概率为零
p0Denom = 2.0; p1Denom = 2.0 #change to 2.0
for i in range(numTrainDocs): #遍历行
if trainCategory[i] == 1: #1 侮辱
p1Num += trainMatrix[i] #所有侮辱文章listj listj[侮辱1]+listj[侮辱2]+listj[侮辱3]
print(p1Num)
p1Denom += sum(trainMatrix[i]) #带侮辱性,list[侮辱总] 出现次数之和
print(p1Denom)
#6篇文档,0.5概率,运行了3次
else:
p0Num += trainMatrix[i] #
p0Denom += sum(trainMatrix[i])
p1Vect = log(p1Num/p1Denom) #change to log() 以e为底自然对数
p0Vect = log(p0Num/p0Denom) #change to log()
return p0Vect,p1Vect,pAbusive#前者p(有该词|是侮辱文),后者p(有该词|是正常文),p(侮辱文)
#p(侮辱问|有该词)=p(有该词|是侮辱文)*p(侮辱文)/p(有该词)
#p(有该词|是侮辱)=p(侮辱文|有该词)*p(有该词)/p(侮辱文)
def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):
p1 = sum(vec2Classify * p1Vec)-log(pClass1) #element-wise mult
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(bagOfWords2VecMN(myVocabList, postinDoc)) #双遍历,每篇单词中在所有单词中出现次数
p0V,p1V,pAb = trainNB0(array(trainMat),array(listClasses))#前者p(有该词|是侮辱文),后者p(有该词|是正常文),p(侮辱文)
testEntry = ['love', 'my', 'dalmation']
thisDoc = array(bagOfWords2VecMN(myVocabList, testEntry))#test在总单词中出现次数
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))
testingNB()
#A 中有2个灰球,2个黑球;B 中有1个灰球,2个黑球
#A|gray=1/2 B/gray=1/3
#A|black=1/2 B/black=2/3
#p(gray|B)=p(gray)*p(B|gray)/p(B)=(3/7)*(1/3)/(3/7)=1/3
#p(有该词|垃圾邮件)=p(垃圾邮件|有该词)*p(有该词)/p(垃圾邮件)
# 有0.5概率是垃圾邮件*(训练的词*垃圾邮件中的词)
#A 垃圾邮件 B好邮件 灰球-好词 黑球-坏词 无法预先判断好词还是坏词
贝叶斯文本分类--python代码
最新推荐文章于 2023-07-04 10:37:08 发布