代码来自: ML In Action
from numpy import *
import re
def load_dataset():
post_list =[['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']]
classes = [0,1,0,1,0,1] # 垃圾与非垃圾邮件
return post_list, classes
# 创建词汇表
# 集合的并集 相当于把 dataset 中的数据去重
def create_vocab_list(dataset):
vocab_set = set([])
for document in dataset:
vocab_set = vocab_set | set(document)
return list(dataset)
# 把 input_set 中在 vocab_list 的返回来
# 这个就是返回了 input_set 中各个词 在 词汇表中的位置
# 将单词转换为数字 便于计算
# -> [0, 0, 1, 0, 0, 1, 1....]
def words_to_vector(vocab_list, input_set):
vector = [0]*len(vocab_list) # 为0的向量
for word in input_set:
if word in vocab_list:
vector[vocab_list.index(word)] = 1
else:
print "the word: %s is not in my Vocabulary!" % word
return vector
# 朴素贝叶斯通常有 贝努利模型实现和多项式模型实现
# 朴素贝叶斯分类器训练函数
# trainMatrix 为 word 转换后的数字 matrix 方便计算
# trainCategory [0,1,0,1,0,1]
def train_NB0(trainMatrix, trainCategory):
numTrainDocs = len(trainMatrix)
numWords = len(trainMatrix[0])
pAbusive = sum(trainCategory)/float(numTrainDocs) # 辱骂性的概率
p0Num = ones(numWords) # 初始化概率 初始化为1
p1Num = ones(numWords) #change to ones()
p0Denom = 2.0 # 防止出现0向量的时候 结果为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) # 防止一堆小数溢出
p0Vect = log(p0Num/p0Denom)
return p0Vect,p1Vect,pAbusive
# 贝叶斯决策
def classify_NB(vec2Classify, p0Vec, p1Vec, pClass1):
# 这里 log + log 实际上就是 ln(x * y)
# 先计算出分向量的概率和
p1 = sum(vec2Classify * p1Vec) + log(pClass1)
p0 = sum(vec2Classify * p0Vec) + log(1.0 - pClass1)
if p1 > p0:
return 1
else:
return 0
def testing_NB():
listOPosts,listClasses = loadDataSet()
myVocabList = createVocabList(listOPosts)
trainMat=[]
for postinDoc in listOPosts:
trainMat.append(setOfWords2Vec(myVocabList, postinDoc))
p0V,p1V,pAb = train_NB0(array(trainMat),array(listClasses))
testEntry = ['love', 'my', 'dalmation']
thisDoc = array(setOfWords2Vec(myVocabList, testEntry))
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)
# 词袋模型 一个词可能在文档中出现多次
def bagOfWords2VecMN(vocabList, inputSet):
returnVec = [0]*len(vocabList)
for word in inputSet:
if word in vocabList:
returnVec[vocabList.index(word)] += 1
return returnVec
def textParse(bigString): #input is big string, #output is word list
listOfTokens = re.split(r'\W*', bigString)
return [tok.lower() for tok in listOfTokens if len(tok) > 2]
# 测试的邮件数据地址:
# https://github.com/start-program/machinelearninginaction/blob/master/Ch04/email.zip
def spam_test():
docList=[]
classList = []
fullText =[]
# 导入解析的文本文件
for i in range(1,26):
wordList = textParse(open('email/spam/%d.txt' % i).read())
docList.append(wordList)
fullText.extend(wordList)
classList.append(1)
wordList = textParse(open('email/ham/%d.txt' % i).read())
docList.append(wordList)
fullText.extend(wordList)
classList.append(0)
print('parse ham and spam success..')
# 随机构建训练集
vocabList = create_vocab_list(docList)#create vocabulary
print('create_vocab_list success..')
trainingSet = range(50)
testSet=[] #create test set
for i in range(10):
randIndex = int(random.uniform(0,len(trainingSet)))
testSet.append(trainingSet[randIndex])
del(trainingSet[randIndex])
trainMat=[]
trainClasses = []
for docIndex in trainingSet:
trainMat.append(bagOfWords2VecMN(vocabList, docList[docIndex]))
trainClasses.append(classList[docIndex])
print('bagOfWords2VecMN success..')
p0V, p1V, pSpam = train_NB0(array(trainMat),array(trainClasses))
print('p0V p1V pSpam success')
# 对测试集进行分类
errorCount = 0
for docIndex in testSet: #classify the remaining items
wordVector = bagOfWords2VecMN(vocabList, docList[docIndex])
result = classify_NB(array(wordVector), p0V, p1V, pSpam)
if result != classList[docIndex]:
errorCount += 1
print "classification: ", result, classList[docIndex]
print ('the error rate is: ', float(errorCount)/len(testSet), errorCount, len(testSet))
#return vocabList,fullText
spam_test()
# classification: 0 1
# classification: 0 0
# classification: 0 0
# classification: 0 1
# classification: 0 0
# classification: 0 0
# classification: 0 1
# classification: 0 1
# classification: 0 1
# classification: 0 0
# ('the error rate is: ', 0.5, 5, 10)