您的位置:首页 > 编程语言 > Python开发

朴素贝叶斯分类器

2017-06-06 09:27 190 查看
朴素贝叶斯模型在文本分类中有着广泛的应用,特别是在互联网新闻分类、垃圾邮件的筛选中。朴素贝叶斯模型假设各个特征向量之间相互独立,这使得模型预测所需要估计的参数规模从幂指数数量级向线性量级减少,极大地节约了内存的消耗和计算时间。但是也正是由于这种强假设的限制,模型训练无法将各个特征之间的联系考量在内,使得模型在其他数据特征关联性较强的分类任务上的性能表现不佳。

本篇博客不讲述朴素贝叶斯模型的理论知识,而是采用一个对新闻文本进行分类的实例。语言是Python3.6,环境是Anaconda3。

1、读取20类新闻文本的数据细节

#导入新闻数据抓取器
from sklearn.datasets import fetch_20newsgroups
#与之前预存数据不同,fetch_20newsgroups需要即时从互联网下载数据
#Downloading dataset from http://people.csail.mit.edu/jrennie/20Newsgroups/20news-bydate.tar.gz (14 MB)
news=fetch_20newsgroups(subset='all')
#查验数据规模和细节
print(len(news.data))
print(news.data[0])


2、20类新闻文本数据分割

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)


3、使用朴素贝叶斯分类器对新闻文本数据进行类别预测
#导入文本特征向量化模块
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)
4、导入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))


5、结果分析
The accuracy of Naive Bayes Classifier is  0.839770797963

                          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

从评估结果中可以看出朴素贝叶斯模型在新闻分类中达到的准确率有83.977%,表现出不错的分类性能。

本博客内容有参考学习《Python机器学习及实践——从零开始通往Kaggle竞赛之路》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息