Python机器学习案例之朴素贝叶斯分类--文本、邮件分类

来源:互联网 发布:苹果系统php集成环境 编辑:程序博客网 时间:2024/06/10 04:28
# -*- coding: utf-8 -*-__author__ = 'gerry''''朴素贝叶斯分类:计算出每个样本在各个类别中的概率,然后划分类别    优点:在数据较少的情况下仍然有效,可以处理多类别问题    缺点:对于输入数据的准备方式较为敏感    适合数据类型:标称型数据    贝叶斯准则:计算条件概率的方法        已知P(x|c),求P(c|x)            p(c|x) = p(x|c)P(c)/p(x)        对于某个点(x,y)来说            p(ci|x.y) = p(x,y|ci)p(ci)/p(x,y)    机器学习的一个重要应用就是文档(新闻报道、用户留言、政府公文)的自动分类''''''    朴素贝叶斯的一般过程:        * 收集数据:可以使用任何方法,这里使用RSS源        * 准备数据:需要数值型数据或者布尔型数据        * 分析数据:有大量特征时,绘制特征的作用不大,此时的直方图的效果更好        * 训练算法:计算不同的独立特征的条件概率        * 测试算法:计算错误概率        * 使用算法:一个常见的朴素贝叶斯应用就是文档的分类。''''''    选取特征工程:        根据统计学可以知道,如果每个特征需要N个样本,那么10个特征将需要N的10次方个样本,对于1000个特征,可想而知,这样所需要的样本数会随着特征数目增大而迅速增长。        但是,如果特征之间相互独立,那么样本数就可以从N^1000减少到N*1000        朴素贝叶斯假设:假设各个样本之间相互独立''''''    Created on Nov 9,2017    @aurhor:Gerry'''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 is not, category labels    return postingList, classVec# 创建一个包含在所有文档中出现的不重复词的列表(选用set集合)def createVocabList(dataSet):    vocabSet = set([])  # create empty set    for document in dataSet:        vocabSet = vocabSet | set(document)  # union of the two sets    return list(vocabSet)def setOfWords2Vec(vocabList, imputSet):    returnVec = [0] * len(vocabList)    for word in imputSet:        if word in vocabList:            returnVec[vocabList.index(word)] = 1        else:            print "the word:%s  is not in my Vocabulary!" % word    return returnVec'''    如何计算概率:        计算每个类别中的文档数目        对每篇训练文档:            对每个类别:                如果词条中出现文档中-->增加该词条的计数值                增加所有词条的计数值            对于每个类别:                对每个词条:                    将该词条的数据除以总词条数目得到条件概率            返回每个类别的条件概率''''''    使用上述计算的数字计算概率'''def trainNB0(trainMatrix, trainCategory):    # trainMatrix:是文档矩阵    # trainCategory:由每篇文章类别标签所构成的向量    numTrainDocs = len(trainMatrix)  # 矩阵的行(即样本数->文章个数)    numWords = len(trainMatrix[0])  # 每篇文章的单词数    pAbusive = sum(trainCategory) / float(numTrainDocs)    # 计算p(wi|c1),p(wi|c2),初始化程序中的分子变量和分母变量    p0Num = ones(numWords);    p1Num = ones(numWords)  # change to ones()    p0Denom = 2.0;    p1Denom = 2.0  # change to 2.0    # 利用贝叶斯分类器对文档进行分类时,要计算多个概率的乘积以获得文档属于某个类别的概率    # 即计算p(w0|1)p(w1|1)p(w1|1).如果其中一个概率为0,那么最后的乘积也为0,为降低这种影响,可以将所有词出现数初始化为1,并将分母初始化为2    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)  # change to log()    p0Vect = log(p0Num / p0Denom)  # change to log()    # 下溢出:这是由于有太多的很小的数相乘造成的p(w0|ci)p(w1|ci)p(w2|ci)...p(wN|ci)时,由于大部分因子都非常小,所以程序会下溢或者得不到正确的答案,因此采取自然对数    return p0Vect, p1Vect, pAbusive# 朴素贝叶斯分类函数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(array(trainMat), array(listClasses))    testEntry = ['love', 'my', 'dalmation']    thisDoc = array(setOfWords2Vec(myVocablist, testEntry))    print testEntry, 'classified as', classifyNB(thisDoc, p0V, p1V, pAb)    testEntry = ['stupid', 'farbage']    thisDoc = array(setOfWords2Vec(myVocablist, testEntry))    print testEntry, 'classified as', classifyNB(thisDoc, p0V, p1V, pAb)'''================================================================================================电子邮件分类    * 收集数据:提供文本文件    * 准备数据:将文本文件解析成词条向量    * 分析数据:检查词条确保解析的正确性    * 训练算法:使用我们之前简历的trainNB0()函数    * 测试算法:使用classifyNB(),并且构建一个新的测试函数来计算文档集的错误率    * 使用算法:构建一个完整的程序对一组文档进行分类,将错误的文档输出到屏幕上'''# 切分词组;接受一个大字符串并将其解析为字符串列表,该函数去掉少于两个字符的字符串,并将所有字符串转换为小写def bagofWords2VecMN(vocabList, inputSet):    returnVec = [0] * len(vocabList)    for word in inputSet:        if word in vocabList:            returnVec[vocabList.index(word)] += 1        return returnVecdef textParse(bigString):    import re    listOfTakens = re.split(r'\W*', bigString)    return [tok.lower() for tok in listOfTakens if len(tok) > 2]# 对贝叶斯垃圾邮件分类器进行自动化处理def spamTest():    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)  # 样本个数(50个)        fullText.extend(wordList)  # 全文数据        classList.append(0)  # 根据每个样本贴标签(选取特征工程,是否是垃圾邮件)    vocabList = createVocabList(docList)  # create vocabulary    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:  # train the classifier (get probs) trainNB0        trainMat.append(bagofWords2VecMN(vocabList, docList[docIndex]))        trainClasses.append(classList[docIndex])    p0V, p1V, pSpam = trainNB0(array(trainMat), array(trainClasses))    errorCount = 0    for docIndex in testSet:  # classify the remaining items        wordVector = bagofWords2VecMN(vocabList, docList[docIndex])        if classifyNB(wordVector, p0V, p1V, pSpam) != classList[docIndex]:            errorCount += 1            print "classification error", docList[docIndex]    print 'the error rate is: ', float(errorCount) / len(testSet)    # return vocabList,fullText'''    案例分析:使用朴素贝叶斯从个人广告中获取区域倾向    * 收集数据:从RSS源收集信息,这里需要对RSS构建一个接口    * 准备数据:将文本文件解析成词条向量    * 分析数据:检查词条确保解析的正确性    * 训练算法:使用我们之前建立的trainNB0()函数    * 测试算法:观察错误率,确保分类器可用,可以修改切分程序,以降低错误率,提高分类结果    * 使用算法:构建一个完整的程序,封装所有内容。给定两个RSS源,该程序会显示最常用的公共词'''# RSS源分类器以及高频词去除函数# 该函数遍历词汇表中的每个词,并统计它在文本汇总出现的次数,然后根据出现的次数从高到低对词典进行排序,最后范围排序最高的30个单词def calcMostFreq(vocabList, fullText):    # 计算出现频率    import operator    freqDict = {}    for token in vocabList:        freqDict[token] = fullText.count(token)    sortedFreq = sorted(freqDict.iteritems(), key=operator.itemgetter(1), reverse=True)    print sortedFreq    return sortedFreq[:30]def localWords(feed1, feed0):    docList = [];    classList = [];    fullText = []    minLen = min(len(feed1['entries']), len(feed0['entries']))    for i in range(minLen):        wordList = textParse(feed1['entries'][i]['summary'])  # 每次访问一条RSS源        docList.append(wordList)        fullText.extend(wordList)        classList.append(1)  # NY is class1        wordList = textParse(feed0['entries'][i]['summary'])        docList.append(wordList)        fullText.extend(wordList)        classList.append(0)    vocabList = createVocabList(docList)  # create vocabulary    top30Words = calcMostFreq(vocabList, fullText)  # remove to 30 words    for pairW in top30Words:        if pairW[0] in vocabList:            vocabList.remove(pairW[0])  # 去掉出现次数最高的那些词    trainingSet = range(2 * minLen);    testSet = []  # create test set    for i in range(20):        randIndex = int(random.uniform(0, len(trainingSet)))        testSet.append(trainingSet[randIndex])        del (trainingSet[randIndex])    trainMat = [];    trainClasses = []    for docIndex in trainingSet:  # train the classifier (get probs) trainNB0        trainMat.append(bagofWords2VecMN(vocabList, docList[docIndex]))        trainClasses.append(classList[docIndex])    p0V, p1V, pSpam = trainNB0(array(trainMat), array(trainClasses))    errorCount = 0    for docIndex in testSet:  # classify the remaint items        wordVector = bagofWords2VecMN(vocabList, docList[docIndex])        if classifyNB(array(wordVector), p0V, p1V, pSpam) != classList[docIndex]:            errorCount += 1    print 'the error rate is:', float(errorCount) / len(testSet)    return vocabList, p0V, p1V# 最具表征性的词汇显示函数def getTopWords(ny, sf):    vocabList, p0V, p1V = localWords(ny, sf)    topNY = [];    topSF = []    for i in range(len(p0V)):        if p0V[i] > -6.0: topSF.append((vocabList[i], p0V[i]))        if p1V[i] > -6.0: topNY.append((vocabList[i], p1V[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]if __name__ == '__main__':    import feedparser    ny = feedparser.parse('http://newyork.craigslist.org/stp/index.rss')    sf = feedparser.parse('http://sfbay.craigslist.org/stp/index.rss')    localWords(ny, sf)    getTopWords(ny, sf)'''    贝叶斯概率以及贝叶斯准则提供发了一种利用已知值来估计未知概率的方法    通过特征之间的条件独立假设,降低对数据量的要求。    利用现代编程语言来实现朴素贝叶斯需要考虑许多实际因素,例如下溢出就是一个问题,可以通过概率取对数来解决,词袋模型在解决文档分类的问题上比词集模型有所提高。'''
阅读全文
0 0