1.朴素贝叶斯
朴素贝叶斯 = 贝叶斯公式 + 条件独立假设
- 抛弃词条间的关联,假设各个词条完全独立,完全基于词袋模型进行计算
2.sklearn实现
# 文档分类
# 朴素贝叶斯
# 从原始语料df中提取出所需的前两章段落
raw12 = df[df.chap.isin([1,2])]
raw12ana = raw12.iloc[list(raw12.txt.apply(len) > 50), :] # 只使用超过50字的段落
raw12ana.reset_index(drop = True, inplace = True)
raw12ana['cleantxt'] = raw12ana.txt.apply(text_cut)
countvec = CountVectorizer()
wordmtx = countvec.fit_transform(raw12ana.cleantxt)
# 将数据集划分为训练集和测试集
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(wordmtx, raw12ana.chap, test_size=0.3, random_state=111)
# 拟合朴素贝叶斯模型
from sklearn import naive_bayes
NBmodel = naive_bayes.MultinomialNB()
# 拟合模型
NBmodel.fit(x_train, y_train)
# 进行验证集预测
NBmodel.predict(x_test)
# 预测准确率
print('训练集:', NBmodel.score(x_train, y_train),
', 验证集:', NBmodel.score(x_test, y_test))
# 模型评估
from sklearn.metrics import classification_report
print(classification_report(y_test, NBmodel.predict(x_test)))