尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
【python】python机器学习实战3:朴素贝叶斯分类器
1.朴素贝叶斯简介第一个问题为什么叫“朴素”是因为在整个形式化过程只做最原始、最简单的假设。朴素贝叶斯的优点在数据较少的情况下仍然有效可以处理多类别问题。缺点是对于输入数据的准备方式较为敏感适用的数据类型标称型数据。网上基本的介绍贝叶斯的理论已经很多了这里我就不再重复赘述了。LZ分享一下原始代码和数据集有兴趣的小伙伴可以自行下载本代码在python2.7版本下运行是没有问题的如果是python3.X版本的可能有些地方会报错这个代码修改请小伙伴自行解决了。链接: https://pan.baidu.com/s/1c228QTM 密码: 35yf2.代码实现#coding:utf-8#导入numpy依赖库fromnumpyimport*#词表到向量的转换函数首先要先构建数据集及其对应的标签defloadDataSet():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 notreturnpostingList,classVecdefcreateVocabList(dataSet):vocabSetset([])#create empty setfordocumentindataSet:#操作符|用于两个集合的并集vocabSetvocabSet|set(document)#union of the two setsreturnlist(vocabSet)#转换成词向量长度和输入的单词列表相同如果在文本中出现那个单词则该单词出现的位置值设为1defsetOfWords2Vec(vocabList,inputSet):returnVec[0]*len(vocabList)forwordininputSet:ifwordinvocabList:returnVec[vocabList.index(word)]1else:printthe word: %s is not in my Vocabulary!%wordreturnreturnVec#朴素贝叶斯分类器训练函数deftrainNB0(trainMatrix,trainCategory):numTrainDocslen(trainMatrix)numWordslen(trainMatrix[0])pAbusivesum(trainCategory)/float(numTrainDocs)p0Numones(numWords);p1Numones(numWords)#change to ones()p0Denom2.0;p1Denom2.0#change to 2.0foriinrange(numTrainDocs):iftrainCategory[i]1:p1NumtrainMatrix[i]p1Denomsum(trainMatrix[i])else:p0NumtrainMatrix[i]p0Denomsum(trainMatrix[i])p1Vectlog(p1Num/p1Denom)#change to log()p0Vectlog(p0Num/p0Denom)#change to log()returnp0Vect,p1Vect,pAbusive#朴素贝叶斯分类函数defclassifyNB(vec2Classify,p0Vec,p1Vec,pClass1):p1sum(vec2Classify*p1Vec)log(pClass1)#element-wise multp0sum(vec2Classify*p0Vec)log(1.0-pClass1)ifp1p0:return1else:return0#文档的词袋模型每当遇到一个单词是它会增加词向量中的对应值而不将对应值设为1设为1的是词集模型defbagOfWords2VecMN(vocabList,inputSet):returnVec[0]*len(vocabList)forwordininputSet:ifwordinvocabList:returnVec[vocabList.index(word)]1returnreturnVec#便利函数deftestingNB():listOPosts,listClassesloadDataSet()myVocabListcreateVocabList(listOPosts)trainMat[]forpostinDocinlistOPosts:trainMat.append(setOfWords2Vec(myVocabList,postinDoc))p0V,p1V,pAbtrainNB0(array(trainMat),array(listClasses))testEntry[love,my,dalmation]thisDocarray(setOfWords2Vec(myVocabList,testEntry))printtestEntry,classified as: ,classifyNB(thisDoc,p0V,p1V,pAb)testEntry[stupid,garbage]thisDocarray(setOfWords2Vec(myVocabList,testEntry))printtestEntry,classified as: ,classifyNB(thisDoc,p0V,p1V,pAb)#切分文本#文件解析完整的垃圾邮件测试函数deftextParse(bigString):#input is big string, #output is word listimportre listOfTokensre.split(r\W*,bigString)return[tok.lower()fortokinlistOfTokensiflen(tok)2]#对贝叶斯垃圾邮件分类器进行自动化处理defspamTest():docList[];classList[];fullText[]foriinrange(1,26):wordListtextParse(open(email/spam/%d.txt%i).read())docList.append(wordList)fullText.extend(wordList)classList.append(1)wordListtextParse(open(email/ham/%d.txt%i).read())docList.append(wordList)fullText.extend(wordList)classList.append(0)vocabListcreateVocabList(docList)#create vocabularytrainingSetrange(50);testSet[]#create test setforiinrange(10):randIndexint(random.uniform(0,len(trainingSet)))testSet.append(trainingSet[randIndex])del(trainingSet[randIndex])trainMat[];trainClasses[]fordocIndexintrainingSet:#train the classifier (get probs) trainNB0trainMat.append(bagOfWords2VecMN(vocabList,docList[docIndex]))trainClasses.append(classList[docIndex])p0V,p1V,pSpamtrainNB0(array(trainMat),array(trainClasses))errorCount0fordocIndexintestSet:#classify the remaining itemswordVectorbagOfWords2VecMN(vocabList,docList[docIndex])ifclassifyNB(array(wordVector),p0V,p1V,pSpam)!classList[docIndex]:errorCount1printclassification error,docList[docIndex]printthe error rate is: ,float(errorCount)/len(testSet)#return vocabList,fullText#RSS源分类器及高频词去除函数#遍历词汇表中每个词并统计它在文本中出现的次数排序后返回前30个单词defcalcMostFreq(vocabList,fullText):importoperator freqDict{}fortokeninvocabList:freqDict[token]fullText.count(token)sortedFreqsorted(freqDict.iteritems(),keyoperator.itemgetter(1),reverseTrue)returnsortedFreq[:30]deflocalWords(feed1,feed0):importfeedparser docList[];classList[];fullText[]minLenmin(len(feed1[entries]),len(feed0[entries]))foriinrange(minLen):wordListtextParse(feed1[entries][i][summary])docList.append(wordList)fullText.extend(wordList)classList.append(1)#NY is class 1wordListtextParse(feed0[entries][i][summary])docList.append(wordList)fullText.extend(wordList)classList.append(0)vocabListcreateVocabList(docList)#create vocabularytop30WordscalcMostFreq(vocabList,fullText)#remove top 30 wordsforpairWintop30Words:ifpairW[0]invocabList:vocabList.remove(pairW[0])trainingSetrange(2*minLen);testSet[]#create test setforiinrange(20):randIndexint(random.uniform(0,len(trainingSet)))testSet.append(trainingSet[randIndex])del(trainingSet[randIndex])trainMat[];trainClasses[]fordocIndexintrainingSet:#train the classifier (get probs) trainNB0trainMat.append(bagOfWords2VecMN(vocabList,docList[docIndex]))trainClasses.append(classList[docIndex])p0V,p1V,pSpamtrainNB0(array(trainMat),array(trainClasses))errorCount0fordocIndexintestSet:#classify the remaining itemswordVectorbagOfWords2VecMN(vocabList,docList[docIndex])ifclassifyNB(array(wordVector),p0V,p1V,pSpam)!classList[docIndex]:errorCount1printthe error rate is: ,float(errorCount)/len(testSet)returnvocabList,p0V,p1V#最具表征性词汇显示函数defgetTopWords(ny,sf):importoperator vocabList,p0V,p1VlocalWords(ny,sf)topNY[];topSF[]foriinrange(len(p0V)):ifp0V[i]-6.0:topSF.append((vocabList[i],p0V[i]))ifp1V[i]-6.0:topNY.append((vocabList[i],p1V[i]))sortedSFsorted(topSF,keylambdapair:pair[1],reverseTrue)printSF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**foriteminsortedSF:printitem[0]sortedNYsorted(topNY,keylambdapair:pair[1],reverseTrue)printNY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**foriteminsortedNY:printitem[0]哈哈完美啦O(∩_∩)O
RELATED

相关推荐

【数据处理】如何处理数据中的缺失值??

【数据处理】如何处理数据中的缺失值??

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📅 2026/10/23 1:01:21
【android】三方库的Android移植

【android】三方库的Android移植

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📅 2026/10/23 1:01:21
【android】Application.mk的文件梳理

【android】Application.mk的文件梳理

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📅 2026/10/23 1:01:21
MORE NEWS

更多资讯

📰

AI岗位暴涨12倍!小白程序员必备:收藏这份2026年高薪跳槽指南

2026年新经济行业招聘市场回温,AI岗位量同比增长12倍,成为招聘主角,平均月薪达60738元。职场门槛提升,岗位“去初级化”,AI能力成为标配。近六成职场人想跳槽,近半数期望涨薪超20%。大模型算法、产品经理等…

📰

具身智能协同演化动力学(120):TVA-World 协同架构虚实域适应机理研究

前沿技术探索:FRA核心定义与设计理念 FRA(Factorized Robotic Agent, FRA,亦称:因式分解机器人智能体)是一套因式化数实融合一体化新型智能系统,核心设计理念是全链路系统解耦,将机器人作业的感…

📰

服务器买了一台又一台?虚拟化到云计算的演进一次讲清

业务每上一个新项目就采购一批服务器,机器越堆越多,利用率却始终上不去——这是传统 IT 最典型的困境。本文沿「传统架构 → 虚拟化 → 云化」这条演进主线,讲清云计算的定义与五大特征,分清 IaaS / PaaS / SaaS 与公有云 / 私有云…

📰

2026年大模型AI校招薪资狂飙!顶尖年薪高达154万,非AI专业也能入行!

2026年AI校招薪资火爆,顶尖大模型算法岗月薪可达5.2万元,年薪最高达154万元。大厂抢人大战激烈,股票期权占比提升。学历不再是关键因素,实际项目经验和技能更受重视。非AI专业人士可通过AI产品、数据工程等方向入行,AI…

📰

企业小白程序员必看:AI 模型如何真正“懂业务”实现落地?

本文深入探讨了企业 AI 发展中普遍存在的“模型不懂业务”问题,指出传统数字化流程只是将企业信息翻译给软件,而 AI 时代需要模型理解现实世界。文章通过一个企业安全 Agent 案例,拆解了 AI 真正进入业务所需理解的五个关键层面:身…

📰

OpenAI 挨个通知了 100 多家组织:给 Agent 补一张出站责任清单

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬