侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python实现的朴素贝叶斯算法经典示例【测试可用】

Python  /  管理员 发布于 7年前   153

本文实例讲述了Python实现的朴素贝叶斯算法。分享给大家供大家参考,具体如下:

代码主要参考机器学习实战那本书,发现最近老外的书确实比中国人写的好,由浅入深,代码通俗易懂,不多说上代码:

#encoding:utf-8'''''Created on 2015年9月6日@author: ZHOUMEIXU204朴素贝叶斯实现过程'''#在该算法中类标签为1和0,如果是多标签稍微改动代码既可import numpy as nppath=u"D:\\Users\\zhoumeixu204\Desktop\\python语言机器学习\\机器学习实战代码  python\\机器学习实战代码\\machinelearninginaction\\Ch04\\"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 not  return postingList,classVecdef createVocabList(dataset):  vocabSet=set([])  for document in dataset:    vocabSet=vocabSet|set(document)  return list(vocabSet)def setOfWordseVec(vocabList,inputSet):  returnVec=[0]*len(vocabList)  for word in inputSet:    if word in vocabList:      returnVec[vocabList.index(word)]=1  #vocabList.index() 函数获取vocabList列表某个元素的位置,这段代码得到一个只包含0和1的列表    else:      print("the word :%s is not in my Vocabulary!"%word)  return returnVeclistOPosts,listClasses=loadDataSet()myVocabList=createVocabList(listOPosts)print(len(myVocabList))print(myVocabList)print(setOfWordseVec(myVocabList, listOPosts[0]))print(setOfWordseVec(myVocabList, listOPosts[3]))#上述代码是将文本转化为向量的形式,如果出现则在向量中为1,若不出现 ,则为0def trainNB0(trainMatrix,trainCategory):  #创建朴素贝叶斯分类器函数  numTrainDocs=len(trainMatrix)  numWords=len(trainMatrix[0])  pAbusive=sum(trainCategory)/float(numTrainDocs)  p0Num=np.ones(numWords);p1Num=np.ones(numWords)  p0Deom=2.0;p1Deom=2.0  for i in range(numTrainDocs):    if trainCategory[i]==1:      p1Num+=trainMatrix[i]      p1Deom+=sum(trainMatrix[i])    else:      p0Num+=trainMatrix[i]      p0Deom+=sum(trainMatrix[i])  p1vect=np.log(p1Num/p1Deom)  #change to log  p0vect=np.log(p0Num/p0Deom)  #change to log  return p0vect,p1vect,pAbusivelistOPosts,listClasses=loadDataSet()myVocabList=createVocabList(listOPosts)trainMat=[]for postinDoc in listOPosts:  trainMat.append(setOfWordseVec(myVocabList, postinDoc))p0V,p1V,pAb=trainNB0(trainMat, listClasses)if __name__!='__main__':  print("p0的概况")  print (p0V)  print("p1的概率")  print (p1V)  print("pAb的概率")  print (pAb)

运行结果:

32
['him', 'garbage', 'problems', 'take', 'steak', 'quit', 'so', 'is', 'cute', 'posting', 'dog', 'to', 'love', 'licks', 'dalmation', 'flea', 'I', 'please', 'maybe', 'buying', 'my', 'stupid', 'park', 'food', 'stop', 'has', 'ate', 'help', 'how', 'mr', 'worthless', 'not']
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0]

# -*- coding:utf-8 -*-#!python2#构建样本分类器testEntry=['love','my','dalmation'] testEntry=['stupid','garbage']到底属于哪个类别import numpy as npdef 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 not  return postingList,classVecdef createVocabList(dataset):  vocabSet=set([])  for document in dataset:    vocabSet=vocabSet|set(document)  return list(vocabSet)def setOfWordseVec(vocabList,inputSet):  returnVec=[0]*len(vocabList)  for word in inputSet:    if word in vocabList:      returnVec[vocabList.index(word)]=1  #vocabList.index() 函数获取vocabList列表某个元素的位置,这段代码得到一个只包含0和1的列表    else:      print("the word :%s is not in my Vocabulary!"%word)  return returnVecdef trainNB0(trainMatrix,trainCategory):  #创建朴素贝叶斯分类器函数  numTrainDocs=len(trainMatrix)  numWords=len(trainMatrix[0])  pAbusive=sum(trainCategory)/float(numTrainDocs)  p0Num=np.ones(numWords);p1Num=np.ones(numWords)  p0Deom=2.0;p1Deom=2.0  for i in range(numTrainDocs):    if trainCategory[i]==1:      p1Num+=trainMatrix[i]      p1Deom+=sum(trainMatrix[i])    else:      p0Num+=trainMatrix[i]      p0Deom+=sum(trainMatrix[i])  p1vect=np.log(p1Num/p1Deom)  #change to log  p0vect=np.log(p0Num/p0Deom)  #change to log  return p0vect,p1vect,pAbusivedef  classifyNB(vec2Classify,p0Vec,p1Vec,pClass1):  p1=sum(vec2Classify*p1Vec)+np.log(pClass1)  p0=sum(vec2Classify*p0Vec)+np.log(1.0-pClass1)  if p1>p0:    return 1  else:    return 0def testingNB():  listOPosts,listClasses=loadDataSet()  myVocabList=createVocabList(listOPosts)  trainMat=[]  for postinDoc in listOPosts:    trainMat.append(setOfWordseVec(myVocabList, postinDoc))  p0V,p1V,pAb=trainNB0(np.array(trainMat),np.array(listClasses))  print("p0V={0}".format(p0V))  print("p1V={0}".format(p1V))  print("pAb={0}".format(pAb))  testEntry=['love','my','dalmation']  thisDoc=np.array(setOfWordseVec(myVocabList, testEntry))  print(thisDoc)  print("vec2Classify*p0Vec={0}".format(thisDoc*p0V))  print(testEntry,'classified as :',classifyNB(thisDoc, p0V, p1V, pAb))  testEntry=['stupid','garbage']  thisDoc=np.array(setOfWordseVec(myVocabList, testEntry))  print(thisDoc)  print(testEntry,'classified as :',classifyNB(thisDoc, p0V, p1V, pAb))if __name__=='__main__':  testingNB()

运行结果:

p0V=[-3.25809654 -2.56494936 -3.25809654 -3.25809654 -2.56494936 -2.56494936
 -3.25809654 -2.56494936 -2.56494936 -3.25809654 -2.56494936 -2.56494936
 -2.56494936 -2.56494936 -1.87180218 -2.56494936 -2.56494936 -2.56494936
 -2.56494936 -2.56494936 -2.56494936 -3.25809654 -3.25809654 -2.56494936
 -2.56494936 -3.25809654 -2.15948425 -2.56494936 -3.25809654 -2.56494936
 -3.25809654 -3.25809654]
p1V=[-2.35137526 -3.04452244 -1.94591015 -2.35137526 -1.94591015 -3.04452244
 -2.35137526 -3.04452244 -3.04452244 -1.65822808 -3.04452244 -3.04452244
 -2.35137526 -3.04452244 -3.04452244 -3.04452244 -3.04452244 -3.04452244
 -3.04452244 -3.04452244 -3.04452244 -2.35137526 -2.35137526 -3.04452244
 -3.04452244 -2.35137526 -2.35137526 -3.04452244 -2.35137526 -2.35137526
 -2.35137526 -2.35137526]
pAb=0.5
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0]
vec2Classify*p0Vec=[-0.         -0.         -0.         -0.         -0.         -0.         -0.
 -0.         -0.         -0.         -0.         -0.         -0.         -0.
 -1.87180218 -0.         -0.         -2.56494936 -0.         -0.         -0.
 -0.         -0.         -0.         -0.         -0.         -0.
 -2.56494936 -0.         -0.         -0.         -0.        ]
['love', 'my', 'dalmation'] classified as : 0
[0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]
['stupid', 'garbage'] classified as : 1

# -*- coding:utf-8 -*-#! python2#使用朴素贝叶斯过滤垃圾邮件# 1.收集数据:提供文本文件# 2.准备数据:讲文本文件见习成词条向量# 3.分析数据:检查词条确保解析的正确性# 4.训练算法:使用我们之前简历的trainNB0()函数# 5.测试算法:使用classifyNB(),并且对建一个新的测试函数来计算文档集的错误率# 6.使用算法,构建一个完整的程序对一组文档进行分类,将错分的文档输出到屏幕上# import re# mySent='this book is the best book on python or M.L. I hvae ever laid eyes upon.'# print(mySent.split())# regEx=re.compile('\\W*')# print(regEx.split(mySent))# emailText=open(path+"email\\ham\\6.txt").read()import numpy as nppath=u"C:\\py\\jb51PyDemo\\src\\Demo\\Ch04\\"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 not  return postingList,classVecdef createVocabList(dataset):  vocabSet=set([])  for document in dataset:    vocabSet=vocabSet|set(document)  return list(vocabSet)def setOfWordseVec(vocabList,inputSet):  returnVec=[0]*len(vocabList)  for word in inputSet:    if word in vocabList:      returnVec[vocabList.index(word)]=1  #vocabList.index() 函数获取vocabList列表某个元素的位置,这段代码得到一个只包含0和1的列表    else:      print("the word :%s is not in my Vocabulary!"%word)  return returnVecdef trainNB0(trainMatrix,trainCategory):  #创建朴素贝叶斯分类器函数  numTrainDocs=len(trainMatrix)  numWords=len(trainMatrix[0])  pAbusive=sum(trainCategory)/float(numTrainDocs)  p0Num=np.ones(numWords);p1Num=np.ones(numWords)  p0Deom=2.0;p1Deom=2.0  for i in range(numTrainDocs):    if trainCategory[i]==1:      p1Num+=trainMatrix[i]      p1Deom+=sum(trainMatrix[i])    else:      p0Num+=trainMatrix[i]      p0Deom+=sum(trainMatrix[i])  p1vect=np.log(p1Num/p1Deom)  #change to log  p0vect=np.log(p0Num/p0Deom)  #change to log  return p0vect,p1vect,pAbusivedef  classifyNB(vec2Classify,p0Vec,p1Vec,pClass1):  p1=sum(vec2Classify*p1Vec)+np.log(pClass1)  p0=sum(vec2Classify*p0Vec)+np.log(1.0-pClass1)  if p1>p0:    return 1  else:    return 0def textParse(bigString):  import re  listOfTokens=re.split(r'\W*',bigString)  return [tok.lower() for tok in listOfTokens if len(tok)>2]def spamTest():  docList=[];classList=[];fullText=[]  for i in range(1,26):    wordList=textParse(open(path+"email\\spam\\%d.txt"%i).read())    docList.append(wordList)    fullText.extend(wordList)    classList.append(1)    wordList=textParse(open(path+"email\\ham\\%d.txt"%i).read())    docList.append(wordList)    fullText.extend(wordList)    classList.append(0)  vocabList=createVocabList(docList)  trainingSet=range(50);testSet=[]  for i in range(10):    randIndex=int(np.random.uniform(0,len(trainingSet)))    testSet.append(trainingSet[randIndex])    del (trainingSet[randIndex])  trainMat=[];trainClasses=[]  for  docIndex in trainingSet:    trainMat.append(setOfWordseVec(vocabList, docList[docIndex]))    trainClasses.append(classList[docIndex])  p0V,p1V,pSpam=trainNB0(np.array(trainMat),np.array(trainClasses))  errorCount=0  for  docIndex in testSet:    wordVector=setOfWordseVec(vocabList, docList[docIndex])    if classifyNB(np.array(wordVector), p0V, p1V, pSpam)!=classList[docIndex]:      errorCount+=1  print 'the error rate is :',float(errorCount)/len(testSet)if __name__=='__main__':  spamTest()

运行结果:

the error rate is : 0.0

其中,path路径所使用到的Ch04文件点击此处本站下载。

注:本文算法源自《机器学习实战》一书。

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。


  • 上一条:
    python 筛选数据集中列中value长度大于20的数据集方法
    下一条:
    Python使用matplotlib和pandas实现的画图操作【经典示例】
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客