#导入新闻数据抓取器
from sklearn.datasets import fetch_20newsgroups
#即时从互联网下载数据
news=fetch_20newsgroups(subset='all')
#共有18846条新闻
len(news.data)
#第一条数据
news.data[0]
from sklearn.cross_validation 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
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)
#性能评估
mnb.score(X_test,y_test)
from sklearn.metrics import classification_report
print(classification_report(y_test,y_predict,target_names=news.target_names))