# 从sklearn.datasets里导入新闻数据抓取器fetch_20newsgroup
from sklearn.datasets import fetch_20newsgroups
# 与之前预存的数据不同,fetch_20newsgroup需要即时从互联网下载数据
news = fetch_20newsgroups(subset='all')
# 查验数据规模和细节
print len(news.data)
print news.data[0]
# 使用sklearn.model_selection里的train_test_split模块用于分割数据
from sklearn.model_selection import train_test_split
# 随机采样25%的数据样本作为测试集
X_train, X_test, y_train, y_test = train_test_split(news.data, news.target, test_size=0.25, random_state=33)
# 从sklearn.feature_extraction.test里导入用于文本特征向量转化模块
from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer()
X_train = vec.fit_transform(X_train)
X_test = vec.transform(X_test)
# 从sklearn.naive_bayes里导入朴素贝叶斯模型
from sklearn.naive_bayes import MultinomialNB
# 使用默认配置初始化朴素贝叶斯模型
mnb = MultinomialNB()
# 利用训练数据对模型参数进行估计
mnb.fit(X_train, y_train)
# 对测试样本进行类别预测,结果存储在变量y_predict中
y_predict = mnb.predict(X_test)
# 从sklearn.metrics里导入classification_report用于详细的分类性能报告
from sklearn.metrics import classification_report
print'The accuracy of Naive Bayes Classifier is', mnb.score(X_test, y_test)
print classification_report(y_test, y_predict, target_names=news.target_names)
fetch_20newsgroups 数据集导入失败: no handlers could be fetch_20newsgroups
下载20news-bydate.pkz, 放到C:\Users[Current user]\scikit_learn_data 文件目录就可以