机器学习入门之机器学习之路: python 朴素贝叶斯分类器 预测新闻类别
小标 2018-09-29 来源 : 阅读 1260 评论 0

摘要:本文主要向大家介绍了机器学习入门之机器学习之路: python 朴素贝叶斯分类器 预测新闻类别,通过具体的内容向大家展现,希望对大家学习机器学习入门有所帮助。

本文主要向大家介绍了机器学习入门之机器学习之路: python 朴素贝叶斯分类器 预测新闻类别,通过具体的内容向大家展现,希望对大家学习机器学习入门有所帮助。

  

使用python3 学习朴素贝叶斯分类api
设计到字符串提取特征向量
欢迎来到我的git下载源代码: https://github.com/linyi0604/kaggle
 

 1 from sklearn.datasets import fetch_20newsgroups
 2 from sklearn.cross_validation import train_test_split
 3 # 导入文本特征向量转化模块
 4 from sklearn.feature_extraction.text import CountVectorizer
 5 # 导入朴素贝叶斯模型
 6 from sklearn.naive_bayes import MultinomialNB
 7 # 模型评估模块
 8 from sklearn.metrics import classification_report
 9 
10 ‘‘‘
11 朴素贝叶斯模型广泛用于海量互联网文本分类任务。
12 由于假设特征条件相互独立,预测需要估计的参数规模从幂指数量级下降接近线性量级,节约内存和计算时间
13 但是 该模型无法将特征之间的联系考虑,数据关联较强的分类任务表现不好。
14 ‘‘‘
15 
16 ‘‘‘
17 1 读取数据部分
18 ‘‘‘
19 # 该api会即使联网下载数据
20 news = fetch_20newsgroups(subset="all")
21 # 检查数据规模和细节
22 # print(len(news.data))
23 # print(news.data[0])
24 ‘‘‘
25 18846
26 
27 From: Mamatha Devineni Ratnam 
28 Subject: Pens fans reactions
29 Organization: Post Office, Carnegie Mellon, Pittsburgh, PA
30 Lines: 12
31 NNTP-Posting-Host: po4.andrew.cmu.edu
32 
33 I am sure some bashers of Pens fans are pretty confused about the lack
34 of any kind of posts about the recent Pens massacre of the Devils. Actually,
35 I am  bit puzzled too and a bit relieved. However, I am going to put an end
36 to non-PIttsburghers‘ relief with a bit of praise for the Pens. Man, they
37 are killing those Devils worse than I thought. Jagr just showed you why
38 he is much better than his regular season stats. He is also a lot
39 fo fun to watch in the playoffs. Bowman should let JAgr have a lot of
40 fun in the next couple of games since the Pens are going to beat the pulp out of Jersey anyway. I was very disappointed not to see the Islanders lose the final
41 regular season game.          PENS RULE!!!
42 ‘‘‘
43 
44 ‘‘‘
45 2 分割数据部分
46 ‘‘‘
47 x_train, x_test, y_train, y_test = train_test_split(news.data,
48                                                     news.target,
49                                                     test_size=0.25,
50                                                     random_state=33)
51 
52 ‘‘‘
53 3 贝叶斯分类器对新闻进行预测
54 ‘‘‘
55 # 进行文本转化为特征
56 vec = CountVectorizer()
57 x_train = vec.fit_transform(x_train)
58 x_test = vec.transform(x_test)
59 # 初始化朴素贝叶斯模型
60 mnb = MultinomialNB()
61 # 训练集合上进行训练, 估计参数
62 mnb.fit(x_train, y_train)
63 # 对测试集合进行预测 保存预测结果
64 y_predict = mnb.predict(x_test)
65 
66 ‘‘‘
67 4 模型评估
68 ‘‘‘
69 print("准确率:", mnb.score(x_test, y_test))
70 print("其他指标:\n",classification_report(y_test, y_predict, target_names=news.target_names))
71 ‘‘‘
72 准确率: 0.8397707979626485
73 其他指标:
74                            precision    recall  f1-score   support
75 
76              alt.atheism       0.86      0.86      0.86       201
77            comp.graphics       0.59      0.86      0.70       250
78  comp.os.ms-windows.misc       0.89      0.10      0.17       248
79 comp.sys.ibm.pc.hardware       0.60      0.88      0.72       240
80    comp.sys.mac.hardware       0.93      0.78      0.85       242
81           comp.windows.x       0.82      0.84      0.83       263
82             misc.forsale       0.91      0.70      0.79       257
83                rec.autos       0.89      0.89      0.89       238
84          rec.motorcycles       0.98      0.92      0.95       276
85       rec.sport.baseball       0.98      0.91      0.95       251
86         rec.sport.hockey       0.93      0.99      0.96       233
87                sci.crypt       0.86      0.98      0.91       238
88          sci.electronics       0.85      0.88      0.86       249
89                  sci.med       0.92      0.94      0.93       245
90                sci.space       0.89      0.96      0.92       221
91   soc.religion.christian       0.78      0.96      0.86       232
92       talk.politics.guns       0.88      0.96      0.92       251
93    talk.politics.mideast       0.90      0.98      0.94       231
94       talk.politics.misc       0.79      0.89      0.84       188
95       talk.religion.misc       0.93      0.44      0.60       158
96 
97              avg / total       0.86      0.84      0.82      4712
98 ‘‘‘

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标人工智能机器学习频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved