机器学习入门实战——朴素贝叶斯实战新闻组数据集

朴素贝叶斯实战新闻组数据集

关于朴素贝叶斯的相关理论知识可查看:朴素贝叶斯法

关于新闻组数据集

20newsgroups数据集是用于文本分类、文本挖据和信息检索研究的国际标准数据集之一。一些新闻组的主题特别相似(e.g. comp.sys.ibm.pc.hardware/comp.sys.mac.hardware),还有一些却完全不相关 (e.g misc.forsale /soc.religion.christian)。

20个新闻组数据集包含大约18000个新闻组,其中20个主题分成两个子集:一个用于训练(或开发),另一个用于测试(或用于性能评估)。训练集和测试集之间的分割是基于特定日期之前和之后发布的消息。

代码实战

首先,还是导入数据集

from sklearn.datasets import fetch_20newsgroups

news = fetch_20newsgroups(subset='all')
print(len(news.data))
print(news.data[0])

我们这里打印出来一个新闻例子,如下

18846
From: Mamatha Devineni Ratnam mr47+@andrew.cmu.edu
Subject: Pens fans reactions
Organization: Post Office, Carnegie Mellon, Pittsburgh, PA
Lines: 12
NNTP-Posting-Host: po4.andrew.cmu.edu

I am sure some bashers of Pens fans are pretty confused about the lack
of any kind of posts about the recent Pens massacre of the Devils. Actually,
I am bit puzzled too and a bit relieved. However, I am going to put an end
to non-PIttsburghers’ relief with a bit of praise for the Pens. Man, they
are killing those Devils worse than I thought. Jagr just showed you why
he is much better than his regular season stats. He is also a lot
fo fun to watch in the playoffs. Bowman should let JAgr have a lot of
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
regular season game. PENS RULE!!!

接下来,划分数据集,还是75%训练集,25%测试集

from sklearn.cross_validation import train_test_split
X_train,X_test,Y_train,Y_test = train_test_split(news.data,news.target,test_size=0.25,random_state=33)

我们需要对文本特征进行提取,我们这里使用CountVectorizer来提取特征。CountVectorizer能够将文本词块化,通过计算词汇的数量来将文本转化成向量(更多文本特征提取内容可查看https://www.cnblogs.com/Haichao-Zhang/p/5220974.html)。然后我们导入模型来学习数据。

from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer()
X_train = vec.fit_transform(X_train)
X_test = vec.transform(X_test)

from sklearn.naive_bayes import MultinomialNB
mnb = MultinomialNB()
mnb.fit(X_train,Y_train)
y_predict = mnb.predict(X_test)

最后,我们还是一样,检验一下模型的准确度

from sklearn.metrics import classification_report
print('The Accuracy of Navie Bayes Classifier is',mnb.score(X_test,Y_test))
print(classification_report(Y_test,y_predict,target_names = news.target_names))

这里写图片描述
代码参考:《Python机器学习及实践:从零开始通往Kaggle竞赛之路》

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
朴素叶斯分类是一种简单而有效的机器学习算法,经常用于文本分类、垃圾邮件过滤等任务。下面演示如何使用Python中的sklearn库实现朴素叶斯分类。 首先,需要安装sklearn库,可以使用以下命令: ``` pip install sklearn ``` 接下来,我们需要导入必要的库和数据集: ```python from sklearn.datasets import fetch_20newsgroups from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.naive_bayes import MultinomialNB # 加载数据集 categories = ['alt.atheism', 'soc.religion.christian', 'comp.graphics', 'sci.med'] twenty_train = fetch_20newsgroups(subset='train', categories=categories, shuffle=True, random_state=42) ``` 在这个例子中,我们使用20个新闻数据集中的四个类别作为我们的训练集。接下来,我们将文本转换为数字特征向量,使用TF-IDF向量化器: ```python # 将文本转换为数字特征向量 tfidf_vectorizer = TfidfVectorizer() X_train_tfidf = tfidf_vectorizer.fit_transform(twenty_train.data) ``` 接下来,我们可以使用朴素叶斯分类器进行训练: ```python # 训练朴素叶斯分类器 clf = MultinomialNB().fit(X_train_tfidf, twenty_train.target) ``` 现在,我们可以使用训练好的分类器进行预测: ```python # 使用分类器进行预测 docs_new = ['God is love', 'OpenGL on the GPU is fast'] X_new_tfidf = tfidf_vectorizer.transform(docs_new) predicted = clf.predict(X_new_tfidf) # 打印预测结果 for doc, category in zip(docs_new, predicted): print('%r => %s' % (doc, twenty_train.target_names[category])) ``` 运行结果如下: ``` 'God is love' => soc.religion.christian 'OpenGL on the GPU is fast' => comp.graphics ``` 这说明我们的朴素叶斯分类器可以正确地将文本分类为相应的类别。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Quanfita

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值