数据导入数据处理构建模型模型测评模型特点分析
本次用到的数据为sklearn自带的数据集—20类新闻文本
#从sklearn.datasets里导入新闻数据抓取器fetch_ 20newsgroups.
from sklearn.datasets import fetch_20newsgroups
#与之前预存的数据不同,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!!!
由代码的输出,可获知该数据共有18846条新闻,不同于前面的样例数据,这些文本数据既没有被设定特征,也没有数字化的量度。因此,在交给朴素贝叶斯分类器学习之前,要对数据做进一步的处理。不过在此之前,仍需要对数据进行分割并且随机采样出一部分用于测试。
#从sklearn.cross val idation导人train test split。
from sklearn.cross_validation import train_test_split
#随机采样25%的数据样本作为测试集。
x_train, x_test, y_train, y_test = train_test_split(news.data, news.target,test_size = 0.25, random_state = 33)
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
"This module will be removed in 0.20.", DeprecationWarning)
将文本转化为特征向量
#从sklearn. feature_ extraction. text里导人用于文本特征向量转化模块。
from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer()
x_train = vec.fit_transform(x_train)
x_test = vec.transform(x_test)
用朴素贝叶斯模型从训练数据中估计参数,最后利用这些概率参数对同样转化为特征向量的测试新闻样本进行类别预测。
#从sklearn.naive bayes里导人朴素贝叶斯模型。
from sklearn.naive_bayes import MultinomialNB
#从使用默认配置初始化朴素贝叶斯模型。
mnb = MultinomialNB ()
#利用训练数据对模型参数进行估计。
mnb.fit(x_train, y_train)
#对测试样本进行类别预测,结果存储在变量y_ predict 中。
y_predict = mnb.predict(x_test)
#从sklearn .metrics里导人classification_ report用于详细的分类性能报告。
from sklearn.metrics import classification_report
print('The accuracy of Naive Bayes Classifier is', mnb.score(x_test, y_test))
print(classification_report(y_test, y_predict, target_names = news.target_names))
The accuracy of Naive Bayes Classifier is 0.8397707979626485
precision recall f1-score support
alt.atheism 0.86 0.86 0.86 201
comp.graphics 0.59 0.86 0.70 250
comp.os.ms-windows.misc 0.89 0.10 0.17 248
comp.sys.ibm.pc.hardware 0.60 0.88 0.72 240
comp.sys.mac.hardware 0.93 0.78 0.85 242
comp.windows.x 0.82 0.84 0.83 263
misc.forsale 0.91 0.70 0.79 257
rec.autos 0.89 0.89 0.89 238
rec.motorcycles 0.98 0.92 0.95 276
rec.sport.baseball 0.98 0.91 0.95 251
rec.sport.hockey 0.93 0.99 0.96 233
sci.crypt 0.86 0.98 0.91 238
sci.electronics 0.85 0.88 0.86 249
sci.med 0.92 0.94 0.93 245
sci.space 0.89 0.96 0.92 221
soc.religion.christian 0.78 0.96 0.86 232
talk.politics.guns 0.88 0.96 0.92 251
talk.politics.mideast 0.90 0.98 0.94 231
talk.politics.misc 0.79 0.89 0.84 188
talk.religion.misc 0.93 0.44 0.60 158
avg / total 0.86 0.84 0.82 4712
通过代码输出,可获知朴素贝叶斯分类器对4712条新闻文本测试样本分类的准确性约为83. 977%,平均精确率、召回率以及F1指标分别为0.86、0.84和0.82。
朴素贝叶斯模型被广泛应用于海量互联网文本分类任务,由于其较强的特征条件独立假设,使得模型预测所需要估计的参数规模从幂指数量级向线性量级减少,极大地节约了内存消耗和计算时间。但是,也正是受这种强假设的限制,模型训练时无法将各个特征之间的联系考量在内,使得该模型在其他数据特征关联性较强的分类任务上的性能表现不佳。