Python机器学习及实践(一、分类学习——朴素贝叶斯分类器)
朴素贝叶斯简介
是一个实用性很强的一个分类器模型,朴素贝叶斯分类器的构造基础是贝叶斯理论。
代码及注释
输入:
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.datasets import fetch_20newsgroups
news = fetch_20newsgroups(subset = "all")
print(len(news.data))
print(news.data[0])
X_train, X_test, y_train, y_test = train_test_split(news.data, news.target, test_size = 0.25, random_state = 33)
vec = CountVectorizer() #将文本转化成特征向量
X_train = vec.fit_transform(X_train)
X_test = vec.transform(X_test)
mnb = MultinomialNB()
mnb.fit(X_train,y_train)
y_predict = mnb.predict(X_test)
print("The accuracy is :", mnb.score(X_test,y_test))
print(classification_report(y_test, y_predict, target_names = news.target_names))
输出:
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.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
accuracy 0.84 4712
macro avg 0.86 0.84 0.82 4712
weighted avg 0.86 0.84 0.82 4712
知识点
1.from sklearn.datasets import fetch_20newsgroups
fetch_20newsgroups是一个需要从网上下载的数据集。
参数中subset有三个值:train、test 、all
2.from sklearn.feature_extraction.text import CountVectorizer
CountVectorizer可以将文本文档集合转换为计数矩阵,变为特征向量。
3.from sklearn.naive_bayes import MultinomialNB
针对多项式模型的朴素贝叶斯(Naive Bayes)分类器。
多项式朴素贝叶斯分类器适合离散特征的分类问题。(例如:文本分类中的单词计数)。
多项式分布一般要求特征计数是整数。然而,实际应用中,如tf-idf这种分数计数也可能有效。