把代码保存于此,python3实现,详解就参考《机器学习实战》(Peter Harrington)啦...
bayes.py:
#refer: http://blog.csdn.net/sinat_17196995/article/details/57412474
from numpy import *
#4-1
#dataset,每行是一个文档
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表示侮辱类,0表示不属于
return postingList,classVec
#所有文档中不重复的词
def createVocabList(dataSet):
vocabSet=set([])
for document in dataSet:
vocabSet=vocabSet|set(document)
return list(vocabSet)
#词集模型
#将 文档 转为 向量 (判断某个词条在文档中是否出现)
#@vocabList: 词汇表
#@inputSet: 文档
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 the vocablury!" % word )#返回文档向量 表示某个词是否在输入文档中出现过 1/0
return returnVec
'''
import bayes
listOPosts,listClasses=bayes.loadDataSet()
myVocabList=bayes.createVocabList(listOPosts)
bayes.setOfWords2Vec(myVocabList,listOPosts[0])
'''
#4-2
#朴素贝叶斯分类训练函数
def trainNB0(trainMatrix,trainCategory):
numTrainDocs = len(trainMatrix) #number of docs
numWords = len(trainMatrix[0]) #number of words
pAbusive = sum(trainCategory)/float(numTrainDocs) # p(class=1)
p0Num = ones(numWords); p1Num = ones(numWords) #class1对应的词汇向量
p0Denom = 2.0; p1Denom = 2.0 #class1总词数
for i in range(numTrainDocs):#遍历每个文档
if trainCategory[i] == 1:#文档属于class1
p1Num += trainMatrix[i]
p1Denom += sum(trainMatrix[i])
else:#文档属于class0
p0Num += trainMatrix[i]
p0Denom += sum(trainMatrix[i])
p1Vect = log( p1Num/p1Denom ) #log(p1Num/p1Denom) #p(w0|c1),p(w1|c1),...,
p0Vect = log( p0Num/p0Denom ) #log(p0Num/p0Denom)
return p0Vect,p1Vect,pAbusive
#返回p0Vec / p1Vec都是矩阵,对应每个词在class0 / class1总体中出现概率
#pAb对应文档属于class1的概率
'''
reload(bayes)
#用 训练数据集合 生成 数据矩阵
trainMat=[]
for postInDoc in listOPosts:
trainMat.append(bayes.setOfWords2Vec(myVocabList,postInDoc))
#查看结果
p0V,p1V,pAb=bayes.trainNB0(trainMat,listClasses)
'''
# 4-3
#对测试样本进行分类
#@vec2Classify: 0,1组合二分类向量,对应词汇表各个词是否出现
def classifyNB(vec2Classify,p0Vec,p1Vec,pClass1):
p1 = sum(vec2Classify * p1Vec)+log(pClass1)
p0 = sum(vec2Classify * p0Vec)+ log(1-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)
#example test 1
testEntry = ['love','my','dalmation']
#判断测试词条在词汇list中是否出现,生成词向量
thisDoc = array(setOfWords2Vec(myVocabList,testEntry))
print(testEntry,'classified as: ',classifyNB(thisDoc,p0V,p1V,pAb))
#example test 2
testEntry = ['stupid','garbage']
thisDoc = array(setOfWords2Vec(myVocabList,testEntry))
print(testEntry,'classified as: ',classifyNB(thisDoc,p0V,p1V,pAb))
#4-4
#词袋模型
#@vocabList: 词汇表
#@inputSet: 文档
#返回 文本中每个词 对应词汇表中 出现的次数
def bagOfWords2VecMN(vocabList,inputSet):
returnVec = [0]*len(vocabList)
for word in inputSet:
if word in vocabList:
returnVec[vocabList.index(word)] += 1
return returnVec
#--------------垃圾邮件过滤----------------
'''
mySent = 'This book is the best on Python or M.L. I have ever laid eyes upon.'
mySent.split()
import re
regEx = re.compile('\\W*')
listOfTokens = regEx.split(mySent)
'''
#4-5
#预处理
def textParse(bigString):
import re
listOfTokens = re.split(r'\W*',bigString) #接收一个大字符串并将其解析为字符串列表
return [ tok.lower() for tok in listOfTokens if len(tok)>2 ]
#过滤邮件 训练+测试
def spamTest():
docList=[]; fullText=[]; classList=[]
for i in range(1,26):
wordList = textParse( open('email/spam/%d.txt' %i).read() )
docList.append(wordList) #矩阵,一行是一个文本
fullText.extend(wordList) #list, 所有单词组成, 去掉数组格式
classList.append(1) #文本类别,class 1
#wordList=textParse(open('email/spam/%d.txt' %i).read()) 书上这行代码有些问题 unicode error
#修改为下面:
wordList = textParse(open('email/ham/%d.txt' % i, "rb").read().decode('GBK', 'ignore'))
docList.append(wordList) #矩阵,一行是一个文本
fullText.extend(wordList) #list, 所有单词组成
classList.append(0) #文本类别,class 0
vocabList = createVocabList(docList)#【step1 -- 创建词列表】
#trainingSet = range(50) python3 del不支持返回数组对象 而是range对象
#修改为下面:
trainingSet = list(range(50)) #training text的编号
testSet = [] #test text的编号
for i in range(10):#【step 2--随机选择10个 作为测试集】
randIndex = int(random.uniform(0,len(trainingSet)))
testSet.append(trainingSet[randIndex])
del(trainingSet[randIndex])
trainMat = [] # training matrix
trainClasses = [] # training文本的类别
for docIndex in trainingSet:
trainMat.append(setOfWords2Vec(vocabList,docList[docIndex]))
trainClasses.append(classList[docIndex])
p0V,p1V,pSpam = trainNB0(array(trainMat),array(trainClasses)) #【step 3 -- 训练分类器】
errorCount = 0#错误数
for docIndex in testSet:#【step 4 -- 测试分类器的效果】
wordVector=setOfWords2Vec(vocabList,docList[docIndex])
if classifyNB(array(wordVector),p0V,p1V,pSpam)!=classList[docIndex]:
errorCount += 1
print('The error rate is:',float(errorCount)/len(testSet))
#------------从广告获取区域倾向------------
'''
#安装feefparser
d:
cd Program Files (x86)
cd python
cd feedparser-develop
python setup.py install
python
import feedparser
ny = feedparser.parse('http://newyork.craigslist.org/stp/index.rss')
'''
#4-6 RSS源分类器及高频词去除函数
# 返回词频最高的30个词
def calcMostFreq(vocabList,fullText):
import operator
freqDict={}
for token in vocabList:
freqDict[token] = fullText.count(token)
sortedFreq = sorted(freqDict.items(),key=operator.itemgetter(1),reverse=True) #降序排列
return sortedFreq[:20]
#训练并测试分类器
def localWords(feed1,feed0):
import feedparser
docList=[]; fullText=[]; classList=[]
minLen = min(len(feed1['entries']),len(feed0['entries']))#每一类取 minLen 个文本
for i in range(minLen):#访问RSS源
wordList = textParse(feed1['entries'][i]['summary'])#访问RSS
docList.append(wordList)#矩阵,一行是一个文本
fullText.extend(wordList) #list, 所有单词组成
classList.append(1) # NY is class 1
wordList = textParse(feed0['entries'][i]['summary'])
docList.append(wordList)
fullText.extend(wordList)
classList.append(0)
vocabList = createVocabList(docList) #[step1--词汇表]
top30Words=calcMostFreq(vocabList,fullText)
for pairW in top30Words:#[step2--去掉出现频数最高的前30个词]
if pairW[0] in vocabList:
vocabList.remove(pairW[0])
trainingSet = list(range(2*minLen)) #training text的编号
testSet = [] #test text的编号
for i in range(20):#[step3--随机选取测试数据]
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]))# training matrix
trainClasses.append(classList[docIndex])
p0V,p1V,pSpam=trainNB0( array(trainMat),array(trainClasses) )#[step4--bayes model]
errorCount=0
for docIndex in testSet:
wordVector = bagOfWords2VecMN(vocabList,docList[docIndex])
if classifyNB( array(wordVector),p0V,p1V,pSpam)!=classList[docIndex]:#[step 5:测试模型]
errorCount+=1
print('The error rate is: ',float(errorCount)/len(testSet))
return vocabList,p0V,p1V
'''
reload(bayes)
ny = feedparser.parse('http://newyork.craigslist.org/stp/index.rss') #city 1
sf = feedparser.parse('http://sfbay.craigslist.org/stp/index.rss') #city 2
vocabList,pSF,pNY=bayes.localWords(ny,sf)
'''
#4-7 最具代表性的词汇显示函数
def getTopWords(ny,sf):
import operator
vocabList,p0V,p1V=localWords(ny,sf)
topNY=[]; topSF=[]
for i in range(len(p0V)):
if p0V[i]>-4.5: topSF.append((vocabList[i],p0V[i]))
if p1V[i]>-4.5: topNY.append((vocabList[i],p0V[i]))
sortedSF=sorted(topSF,key=lambda pair: pair[1],reverse=True)
print("SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**")
for item in sortedSF:
print (item[0])
sortedNY = sorted(topNY, key=lambda pair: pair[1], reverse=True)
print ("NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**")
for item in sortedNY:
print (item[0])
'''
reload(bayes)
bayes.getTopWords(ny,sf)
'''