标签:style blog http io color ar os sp for
朴素贝叶斯
比如我们想判断一个邮件是不是垃圾邮件,那么我们知道的是这个邮件中的词的分布,那么我们还要知道:垃圾邮件中某些词的出现是多少,就可以利用贝叶斯定理得到。
朴素贝叶斯分类器中的一个假设是:每个特征同等重要
loadDataSet()
createVocabList(dataSet)
setOfWords2Vec(vocabList, inputSet)
bagOfWords2VecMN(vocabList, inputSet)
trainNB0(trainMatrix,trainCatergory)
classifyNB(vec2Classify, p0Vec, p1Vec, pClass1)
1 #coding=utf-8 2 from numpy import * 3 def loadDataSet(): 4 postingList=[[‘my‘, ‘dog‘, ‘has‘, ‘flea‘, ‘problems‘, ‘help‘, ‘please‘], 5 [‘maybe‘, ‘not‘, ‘take‘, ‘him‘, ‘to‘, ‘dog‘, ‘park‘, ‘stupid‘], 6 [‘my‘, ‘dalmation‘, ‘is‘, ‘so‘, ‘cute‘, ‘I‘, ‘love‘, ‘him‘], 7 [‘stop‘, ‘posting‘, ‘stupid‘, ‘worthless‘, ‘garbage‘], 8 [‘mr‘, ‘licks‘, ‘ate‘, ‘my‘, ‘steak‘, ‘how‘, ‘to‘, ‘stop‘, ‘him‘], 9 [‘quit‘, ‘buying‘, ‘worthless‘, ‘dog‘, ‘food‘, ‘stupid‘]] 10 classVec = [0,1,0,1,0,1] #1 is abusive, 0 not 11 return postingList,classVec 12 13 #创建一个带有所有单词的列表 14 def createVocabList(dataSet): 15 vocabSet = set([]) 16 for document in dataSet: 17 vocabSet = vocabSet | set(document) 18 return list(vocabSet) 19 20 def setOfWords2Vec(vocabList, inputSet): 21 retVocabList = [0] * len(vocabList) 22 for word in inputSet: 23 if word in vocabList: 24 retVocabList[vocabList.index(word)] = 1 25 else: 26 print ‘word ‘,word ,‘not in dict‘ 27 return retVocabList 28 29 #另一种模型 30 def bagOfWords2VecMN(vocabList, inputSet): 31 returnVec = [0]*len(vocabList) 32 for word in inputSet: 33 if word in vocabList: 34 returnVec[vocabList.index(word)] += 1 35 return returnVec 36 37 def trainNB0(trainMatrix,trainCatergory): 38 numTrainDoc = len(trainMatrix) 39 numWords = len(trainMatrix[0]) 40 pAbusive = sum(trainCatergory)/float(numTrainDoc) 41 #防止多个概率的成绩当中的一个为0 42 p0Num = ones(numWords) 43 p1Num = ones(numWords) 44 p0Denom = 2.0 45 p1Denom = 2.0 46 for i in range(numTrainDoc): 47 if trainCatergory[i] == 1: 48 p1Num +=trainMatrix[i] 49 p1Denom += sum(trainMatrix[i]) 50 else: 51 p0Num +=trainMatrix[i] 52 p0Denom += sum(trainMatrix[i]) 53 p1Vect = log(p1Num/p1Denom)#处于精度的考虑,否则很可能到限归零 54 p0Vect = log(p0Num/p0Denom) 55 return p0Vect,p1Vect,pAbusive 56 57 def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1): 58 p1 = sum(vec2Classify * p1Vec) + log(pClass1) #element-wise mult 59 p0 = sum(vec2Classify * p0Vec) + log(1.0 - pClass1) 60 if p1 > p0: 61 return 1 62 else: 63 return 0 64 65 def testingNB(): 66 listOPosts,listClasses = loadDataSet() 67 myVocabList = createVocabList(listOPosts) 68 trainMat=[] 69 for postinDoc in listOPosts: 70 trainMat.append(setOfWords2Vec(myVocabList, postinDoc)) 71 p0V,p1V,pAb = trainNB0(array(trainMat),array(listClasses)) 72 testEntry = [‘love‘, ‘my‘, ‘dalmation‘] 73 thisDoc = array(setOfWords2Vec(myVocabList, testEntry)) 74 print testEntry,‘classified as: ‘,classifyNB(thisDoc,p0V,p1V,pAb) 75 testEntry = [‘stupid‘, ‘garbage‘] 76 thisDoc = array(setOfWords2Vec(myVocabList, testEntry)) 77 print testEntry,‘classified as: ‘,classifyNB(thisDoc,p0V,p1V,pAb) 78 79 80 def main(): 81 testingNB() 82 83 if __name__ == ‘__main__‘: 84 main()
标签:style blog http io color ar os sp for
原文地址:http://www.cnblogs.com/MrLJC/p/4102737.html