朴素贝叶斯-新闻分类

来源:互联网 发布:守望先锋左上角6个数据 编辑:程序博客网 时间:2024/05/23 21:17
朴素贝叶斯分类器的构造基础是基于贝叶斯定理与特征条件独立假设的分类方法,与基于线性假设的模型(线性分类器和支持向量机分类器)不同。最为广泛的两种分类模型是决策树模型(Decision Tree Model)和朴素贝叶斯模型(Naive Bayesian Model,NBM)。
朴素贝叶斯有着广泛的实际应用环境,特别是在文本分类的任务中,包括新闻的分类,垃圾邮件的筛选。

下面使用经典的20类新闻文本作为试验数据:


Python源码:

#coding=utf-8#load news datafrom sklearn.datasets import fetch_20newsgroups#-------------from sklearn.cross_validation import train_test_split#-------------from sklearn.feature_extraction.text import CountVectorizer#-------------from sklearn.naive_bayes import MultinomialNB#-------------from sklearn.metrics import classification_report#-------------download datanews=fetch_20newsgroups(subset='all')print len(news.data)print news.data[0]#-------------split data#75% training set,25% testing setX_train,X_test,y_train,y_test=train_test_split(news.data,news.target,test_size=0.25,random_state=33)#-------------transfer data to vectorvec=CountVectorizer()X_train=vec.fit_transform(X_train)#X_test=vec.fit_transform(X_test)  raise ValueError('dimension mismatch')vectorizer_test = CountVectorizer(vocabulary=vec.vocabulary_)X_test = vectorizer_test.transform(X_test)#-------------training#initialize NB model with default configmnb=MultinomialNB()#training modelmnb.fit(X_train,y_train)#run on test datay_predict=mnb.predict(X_test)#-------------performanceprint 'The Accuracy is',mnb.score(X_test,y_test)print classification_report(y_test,y_predict,target_names=news.target_names)
Result:

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!!!

The Accuracy 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


该数据共有18846条新闻,这些数据既没有被设定特征,也没用数字化的度量。因此,交给朴素贝叶斯分类起学习之情,要对数据做预处理:将文本转化为特征向量,然后再用朴素贝叶斯模型从训练数据中估计参数,最后利用这些概率参数对同样转化成特征向量的测试样本进行类别预测。
算法特点:
朴素贝叶斯模型被广发用于海量互联网文本分类任务。由于较强的特征条件独立假设,使得模型预测所需要估计的参数规模从幂指数量级向线性量级减少,极大地节约了内存消耗和计算时间。但正是手这种强假设的限制,模型训练时,无法将各个特征之间的联系考量在内,使得该模型在其他数据特征关联性较强的分类任务上的性能表现不佳。

原创粉丝点击