利用朴素贝叶斯算法识别垃圾邮件
来源:互联网 发布:淘宝助理怎么导入宝贝 编辑:程序博客网 时间:2024/06/06 01:11
转载自:http://blog.csdn.net/wowcplusplus/article/details/25190809
朴素贝叶斯算法是被工业界广泛应用的机器学习算法,它有较强的数学理论基础,在一些典型的应用中效果显著。朴素贝叶斯算法基于概率论的贝叶斯理论。该理论的核心公式如下:
式中,表示某种分类,则表示已知的情况下类型为的条件概率。我们求出各个类别下的,然后比较它们的大小,以概率最大的作为最后的类别,以此达到分类的目的。下面我们来看如何计算这些条件概率。
已知,则。朴素贝叶斯假定互为独立变量,则。而(为指示函数,存在则为1,不存在则为0),和都可用训练数据直接统计得出。故可依据上述分析求得的大小。又由于对所有类别都是固定大小,所以比较条件概率的大小等同于比较的大小。这就是朴素贝叶斯的数学原理。
下面,我们以朴素贝叶斯的一个典型应用——过滤垃圾邮件来展示该算法的python实现。
现在,我们有25件垃圾邮件和25件正常邮件,如何使用这些邮件作为训练数据得到过滤垃圾邮件的朴素贝叶斯模型呢?首先,我们用各邮件的词组成词向量,表示在邮件中出现过的词,再计算与孰大孰小即可决定这是封正常邮件(ham)还是封垃圾邮件(spam)。
第一步,我们将邮件转换为numpy的array形式,使用如下函数:
- def file2array(filename):
- fileReader=open(filename,'r').read()
- listOfWord=re.split(r'\W*',fileReader)
- fileArray=[word.lower() for word in listOfWord if len(word)>3]
- return fileArray
- def getAllInfo():
- allTextMat=[]
- allTypeArray=[]
- testMat=[]
- testTypeArray=[]
- for i in range(1,26):
- allTextMat.append(file2array('email/spam/%d.txt'%i))
- allTypeArray.append(1)
- allTextMat.append(file2array('email/ham/%d.txt'%i))
- allTypeArray.append(0)
- for i in range(5):
- randIndex=int(random.uniform(0,len(allTextMat)))
- testMat.append(allTextMat[randIndex])
- testTypeArray.append(allTypeArray[randIndex])
- del(allTextMat[randIndex])
- del(allTypeArray[randIndex])
- return allTextMat,allTypeArray,testMat,testTypeArray
- def getWordList(allTextMat):
- wordSet=set()
- for textVec in allTextMat:
- wordSet|=set(textVec)
- return list(wordSet)
- def getCountList(wordList,allTextMat,allTypeArray):
- wordListLen=len(wordList)
- totalCntList=ones(wordListLen)
- totalCntList*=2
- p0CntList=ones(wordListLen)
- p1CntList=ones(wordListLen)
- order=0
- p0Cnt=0
- p1Cnt=0
- for textVec in allTextMat:
- for word in textVec:
- wordPos=wordList.index(word)
- totalCntList[wordPos]+=1
- if allTypeArray[order]==1:
- p1CntList[wordPos]+=1
- p1Cnt+=1
- elif allTypeArray[order]==0:
- p0CntList[wordPos]+=1
- p0Cnt+=1
- order+=1
- p0=float(p0Cnt)/(p0Cnt+p1Cnt)
- p1=1-p0
- return totalCntList,p0CntList,p1CntList,p0,p1
- def bayesClassify(testMat,testTypeArray,totalCntList,p0CntList,p1CntList,p0,p1,wordList):
- docIndex=0
- errorCnt=0
- for testVec in testMat:
- sum0=0.0
- sum1=0.0
- for word in testVec:
- if word not in wordList:
- continue
- wordPos=wordList.index(word)
- sum0+=log(float(p0CntList[wordPos]/totalCntList[wordPos]))
- sum1+=log(float(p1CntList[wordPos]/totalCntList[wordPos]))
- sum0+=log(p0)
- sum1+=log(p1)
- decType=0 if sum0>sum1 else 1
- if decType != testTypeArray[docIndex]:
- errorCnt+=1
- print sum0,sum1
- docIndex+=1
- return errorCnt
- import os
- import re
- from numpy import *
- def file2array(filename):
- fileReader=open(filename,'r').read()
- listOfWord=re.split(r'\W*',fileReader)
- fileArray=[word.lower() for word in listOfWord if len(word)>3]
- return fileArray
- def getAllInfo():
- allTextMat=[]
- allTypeArray=[]
- testMat=[]
- testTypeArray=[]
- for i in range(1,26):
- allTextMat.append(file2array('email/spam/%d.txt'%i))
- allTypeArray.append(1)
- allTextMat.append(file2array('email/ham/%d.txt'%i))
- allTypeArray.append(0)
- for i in range(5):
- randIndex=int(random.uniform(0,len(allTextMat)))
- testMat.append(allTextMat[randIndex])
- testTypeArray.append(allTypeArray[randIndex])
- del(allTextMat[randIndex])
- del(allTypeArray[randIndex])
- return allTextMat,allTypeArray,testMat,testTypeArray
- def getWordList(allTextMat):
- wordSet=set()
- for textVec in allTextMat:
- wordSet|=set(textVec)
- return list(wordSet)
- def getCountList(wordList,allTextMat,allTypeArray):
- wordListLen=len(wordList)
- totalCntList=ones(wordListLen)
- totalCntList*=2
- p0CntList=ones(wordListLen)
- p1CntList=ones(wordListLen)
- order=0
- p0Cnt=0
- p1Cnt=0
- for textVec in allTextMat:
- for word in textVec:
- wordPos=wordList.index(word)
- totalCntList[wordPos]+=1
- if allTypeArray[order]==1:
- p1CntList[wordPos]+=1
- p1Cnt+=1
- elif allTypeArray[order]==0:
- p0CntList[wordPos]+=1
- p0Cnt+=1
- order+=1
- p0=float(p0Cnt)/(p0Cnt+p1Cnt)
- p1=1-p0
- return totalCntList,p0CntList,p1CntList,p0,p1
- def bayesClassify(testMat,testTypeArray,totalCntList,p0CntList,p1CntList,p0,p1,wordList):
- docIndex=0
- errorCnt=0
- for testVec in testMat:
- sum0=0.0
- sum1=0.0
- for word in testVec:
- if word not in wordList:
- continue
- wordPos=wordList.index(word)
- sum0+=log(float(p0CntList[wordPos]/totalCntList[wordPos]))
- sum1+=log(float(p1CntList[wordPos]/totalCntList[wordPos]))
- sum0+=log(p0)
- sum1+=log(p1)
- decType=0 if sum0>sum1 else 1
- if decType != testTypeArray[docIndex]:
- errorCnt+=1
- print sum0,sum1
- docIndex+=1
- return errorCnt
- def main():
- allTextMat,allTypeArray,testMat,testTypeArray=getAllInfo()
- wordList=getWordList(allTextMat)
- totalCntList,p0CntList,p1CntList,p0,p1=getCountList(wordList,allTextMat,allTypeArray)
- print bayesClassify(testMat,testTypeArray,totalCntList,p0CntList,p1CntList,p0,p1,wordList)
- if __name__=='__main__':
- main()
以上就是贝叶斯算法的基本介绍。作为本系列的开篇之作,我在表述上可能会有不当之处,还请各位同学在评论中指正。
- 利用朴素贝叶斯算法识别垃圾邮件
- 朴素贝叶斯算法----垃圾邮件识别
- 机器学习实例一:利用朴素贝叶斯算法识别垃圾邮件
- 利用朴素贝叶斯算法实现垃圾邮件的过滤,并结合Adaboost改进该算法
- 机器学习手记[3]---朴素贝叶斯识别垃圾邮件的应用
- 朴素贝叶斯---过滤垃圾邮件
- 朴素贝叶斯过滤垃圾邮件
- 利用朴素贝叶斯(Navie Bayes)进行垃圾邮件分类
- 利用朴素贝叶斯和多线程做垃圾邮件分类
- 朴素贝叶斯算法在垃圾邮件过滤中的应用
- 【机器学习实战二:朴素贝叶斯算法之过滤垃圾邮件】
- 【机器学习实战二:朴素贝叶斯算法之过滤垃圾邮件】
- R 朴素贝叶斯 垃圾邮件分类
- 朴素贝叶斯与垃圾邮件分类
- 基于朴素贝叶斯的垃圾邮件检测
- 朴素贝叶斯实现垃圾邮件分类------matlab实现
- 基于朴素贝叶斯的垃圾邮件检测
- [机器学习实战]--朴素贝叶斯过滤垃圾邮件
- bbPress 2.5.1 汉化中文版--wordpress插件
- DOS中切换盘符及目录
- 常用的DIV+CSS网站布局的基本框架结构-完整版
- gdb调试程序
- 关于返回405错误。
- 利用朴素贝叶斯算法识别垃圾邮件
- [HTML5&CSS3]20 个酷炫 应用及源码
- marvell 1920开机过程
- A. SwapSort
- [小技巧] gdb里设定动态库的位置
- 第一篇
- 25条div+CSS编程提醒及小技巧整理
- 内核模块USBIP协议描述
- 知名linux网站及论坛推荐