使用特征选择训练朴素贝叶斯
现在,您将使用上一个练习中的选择结果,重新运行第 3 章末尾实现过的朴素贝叶斯文本分类模型:volunteer 数据集中的 title 和 category_desc 列。
本练习是课程的一部分
Python 中的机器学习预处理
练习说明
- 对
filtered_text文本向量与y标签(即category_desc标签)使用train_test_split(),并将y传给stratify参数,因为类别分布不均衡。 - 将
nb朴素贝叶斯模型拟合到X_train和y_train。 - 计算
nb的测试集准确率。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Split the dataset according to the class distribution of category_desc
X_train, X_test, y_train, y_test = ____(____.toarray(), ____, stratify=____, random_state=42)
# Fit the model to the training data
nb.____
# Print out the model's accuracy
print(nb.____)