基于贝叶斯决策理论的分类方法
优点:在数据较少的情况下仍然有效,可以处理多类别问题。
缺点:对于输入数据的准备方式较为敏感。
适用数据:标称型数据。
使用Python进行文本分类
准备数据:从文本中构建词向量
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] return postingList, classVec
def createVocabList(dataSet): vocabSet = set([]) for document in dataSet: vocabSet = vocabSet | set(document) 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
listOPosts, listClasses = loadDataSet() myVocabList = createVocabList(listOPosts) myVocabList
output ['quit', 'him', 'is', 'food', 'to', 'so', 'please', 'maybe', 'love', 'problems', 'flea', 'park', 'stop', 'not', 'how', 'take', 'dog', 'has', 'i', 'my', 'dalmation', 'garbage', 'ate', 'buying', 'steak', 'mr', 'worthless', 'stupid', 'cute', 'help', 'licks', 'posting']
setOfWords2Vec(myVocabList, listOPosts[0])
output
[0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
训练算法:从词向量计算概率
from numpy import * def trainNB0(trainMatrix, trainCategory): numTrainDocs = len(trainMatrix) numWords = len(trainMatrix[0]) pAbusive = sum(trainCategory) / float(numTrainDocs) p0Num = ones(numWords) 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) p0Vect = log(p0Num / p0Denom) return p0Vect, p1Vect, pAbusive
listOPosts, listClasses = loadDataSet() myVocabList = createVocabList(listOPosts) trainMat = [] for postinDoc in listOPosts: trainMat.append(setOfWords2Vec(myVocabList, postinDoc))
listOPosts
output
[['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']]
trainMat
len(trainMat[0])
output
32
trainMatrix = trainMat trainCategory = listClasses numTrainDocs = len(trainMatrix) numWords = len(trainMatrix[0]) pAbusive = sum(trainCategory) / float(numTrainDocs) p0Num = ones(numWords) p1Num = ones(numWords) p0Denom = 2.0 p1Denom = 2.0 for i in range(numTrainDocs): if trainCategory[i] == 1: p1Num += trainMatrix[i] print(p1Num) p1Denom += sum(trainMatrix[i]) print(p1Denom) else: p0Num += trainMatrix[i] print(p0Num) p0Denom += sum(trainMatrix[i]) print(p0Denom) p1Vect = log(p1Num / p1Denom) p0Vect = log(p0Num / p0Denom)
output
[1. 1. 1. 1. 1. 1. 2. 1. 1. 2. 2. 1. 1. 1. 1. 1. 2. 2. 1. 2. 1. 1. 1. 1. 1. 1. 1. 1. 1. 2. 1. 1.] 9.0 [1. 2. 1. 1. 2. 1. 1. 2. 1. 1. 1. 2. 1. 2. 1. 2. 2. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 2. 1. 1. 1. 1.] 10.0 [1. 2. 2. 1. 1. 2. 2. 1. 2. 2. 2. 1. 1. 1. 1. 1. 2. 2. 2. 3. 2. 1. 1. 1. 1. 1. 1. 1. 2. 2. 1. 1.] 17.0 [1. 2. 1. 1. 2. 1. 1. 2. 1. 1. 1. 2. 2. 2. 1. 2. 2. 1. 1. 1. 1. 2. 1. 1. 1. 1. 2. 3. 1. 1. 1. 2.] 15.0 [1. 3. 2. 1. 2. 2. 2. 1. 2. 2. 2. 1. 2. 1. 2. 1. 2. 2. 2. 4. 2. 1. 2. 1. 2. 2. 1. 1. 2. 2. 2. 1.] 26.0 [2. 2. 1. 2. 2. 1. 1. 2. 1. 1. 1. 2. 2. 2. 1. 2. 3. 1. 1. 1. 1. 2. 1. 2. 1. 1. 3. 4. 1. 1. 1. 2.] 21.0
p0V, p1V, pAb = trainNB0(trainMat, listClasses)
pAb
output
0.5
p0V
output
array([-3.25809654, -2.15948425, -2.56494936, -3.25809654, -2.56494936, -2.56494936, -2.56494936, -3.25809654, -2.56494936, -2.56494936, -2.56494936, -3.25809654, -2.56494936, -3.25809654, -2.56494936, -3.25809654, -2.56494936, -2.56494936, -2.56494936, -1.87180218, -2.56494936, -3.25809654, -2.56494936, -3.25809654, -2.56494936, -2.56494936, -3.25809654, -3.25809654, -2.56494936, -2.56494936, -2.56494936, -3.25809654])
p1V
output
array([-2.35137526, -2.35137526, -3.04452244, -2.35137526, -2.35137526, -3.04452244, -3.04452244, -2.35137526, -3.04452244, -3.04452244, -3.04452244, -2.35137526, -2.35137526, -2.35137526, -3.04452244, -2.35137526, -1.94591015, -3.04452244, -3.04452244, -3.04452244, -3.04452244, -2.35137526, -3.04452244, -2.35137526, -3.04452244, -3.04452244, -1.94591015, -1.65822808, -3.04452244, -3.04452244, -3.04452244, -2.35137526])
测试算法:根据现实情况修改分类器
def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1): p1 = sum(vec2Classify * p1Vec) + log(pClass1) 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(setOfWords2Vec(myVocabList, postinDoc)) p0V, p1V, pAb = trainNB0(trainMat, listClasses) testEntry = ['love', 'my', 'dalmation'] thisDoc = setOfWords2Vec(myVocabList, testEntry) print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb)) testEntry = ['stupid', 'garbage'] thisDoc = setOfWords2Vec(myVocabList, testEntry) print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb)) testEntry = ['stupid'] thisDoc = setOfWords2Vec(myVocabList, testEntry) print(testEntry, 'classified as: ', classifyNB(thisDoc, p0V, p1V, pAb))
testingNB()
output
['love', 'my', 'dalmation'] classified as: 0 ['stupid', 'garbage'] classified as: 1 ['stupid'] classified as: 1