使用CountVectorizer並且不去掉停用詞的條件下,對文本特徵進行量化的樸素貝葉斯分類性能測試

from sklearn.datasets import fetch_20newsgroups
news = fetch_20newsgroups()

from sklearn.model_selection 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)

from sklearn.feature_extraction.text import CountVectorizer
count_vec = CountVectorizer()
x_count_train = count_vec.fit_transform(x_train)
x_count_test = count_vec.transform(x_test)

from sklearn.naive_bayes import MultinomialNB
mnb_count = MultinomialNB()
mnb_count.fit(x_count_train, y_train)
print('The accuracy of classifying 20nesgroups using Naive Bayes(CountVectorizer without filtering stopwords):', mnb_count.score(x_count_test, y_test))
y_count_predict = mnb_count.predict(x_count_test)

from sklearn.metrics import classification_report
print(classification_report(y_test, y_count_predict, target_names=news.target_names))

運行結果如下:

The accuracy of classifying 20nesgroups using Naive Bayes(CountVectorizer without filtering stopwords): 0.831742665253
                          precision    recall  f1-score   support

             alt.atheism       0.89      0.88      0.88       108
           comp.graphics       0.62      0.86      0.72       130
 comp.os.ms-windows.misc       0.95      0.13      0.23       163
comp.sys.ibm.pc.hardware       0.56      0.77      0.64       141
   comp.sys.mac.hardware       0.91      0.83      0.87       145
          comp.windows.x       0.71      0.91      0.80       141
            misc.forsale       0.95      0.67      0.78       159
               rec.autos       0.87      0.90      0.89       139
         rec.motorcycles       0.96      0.94      0.95       153
      rec.sport.baseball       0.97      0.89      0.93       141
        rec.sport.hockey       0.96      0.97      0.96       148
               sci.crypt       0.75      0.99      0.85       143
         sci.electronics       0.90      0.79      0.84       160
                 sci.med       0.95      0.89      0.92       158
               sci.space       0.91      0.91      0.91       149
  soc.religion.christian       0.79      0.97      0.87       157
      talk.politics.guns       0.83      0.95      0.89       134
   talk.politics.mideast       0.89      0.99      0.94       133
      talk.politics.misc       0.78      0.91      0.84       130
      talk.religion.misc       0.98      0.53      0.68        97


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章