分别使用CountVectorizer与TfidfVectorizer, 并且去掉停用词的条件下,对文本特征进行量化的朴素贝叶斯分类性能测试

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
from sklearn.feature_extraction.text import TfidfVectorizer

count_filter_vec, tfidf_filter_vec = CountVectorizer(analyzer='word', stop_words='english'), TfidfVectorizer(analyzer='word', stop_words='english')

x_count_filter_train = count_filter_vec.fit_transform(x_train)
x_count_filter_test = count_filter_vec.transform(x_test)

x_tfidf_filter_train = tfidf_filter_vec.fit_transform(x_train)
x_tfidf_filter_test = tfidf_filter_vec.transform(x_test)

from sklearn.naive_bayes import MultinomialNB

mnb_count_filter = MultinomialNB()
mnb_count_filter.fit(x_count_filter_train, y_train)
y_count_filter_predict = mnb_count_filter.predict(x_count_filter_test)


mnb_tfidf_filter = MultinomialNB()
mnb_tfidf_filter.fit(x_tfidf_filter_train, y_train)
y_tfidf_filter_predict = mnb_tfidf_filter.predict(x_tfidf_filter_test)


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

运行结果如下:

                          precision    recall  f1-score   support

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

             avg / total       0.88      0.86      0.85      2829

                          precision    recall  f1-score   support

             alt.atheism       0.90      0.88      0.89       108
           comp.graphics       0.80      0.86      0.83       130
 comp.os.ms-windows.misc       0.91      0.76      0.83       163
comp.sys.ibm.pc.hardware       0.70      0.83      0.76       141
   comp.sys.mac.hardware       0.92      0.88      0.90       145
          comp.windows.x       0.86      0.88      0.87       141
            misc.forsale       0.92      0.78      0.84       159
               rec.autos       0.90      0.95      0.92       139
         rec.motorcycles       0.92      0.95      0.94       153
      rec.sport.baseball       0.95      0.94      0.94       141
        rec.sport.hockey       0.91      0.99      0.95       148
               sci.crypt       0.81      0.99      0.89       143
         sci.electronics       0.92      0.80      0.86       160
                 sci.med       0.98      0.89      0.93       158
               sci.space       0.88      0.95      0.91       149
  soc.religion.christian       0.72      0.98      0.83       157
      talk.politics.guns       0.85      0.94      0.89       134
   talk.politics.mideast       0.94      1.00      0.97       133
      talk.politics.misc       0.98      0.78      0.87       130
      talk.religion.misc       1.00      0.35      0.52        97

             avg / total       0.89      0.88      0.87      2829


你可能感兴趣的:(python学习之路)