开始使用免费开始使用

使用特征选择训练朴素贝叶斯

现在,您将使用上一个练习中的选择结果,重新运行第 3 章末尾实现过的朴素贝叶斯文本分类模型:volunteer 数据集中的 titlecategory_desc 列。

本练习是课程的一部分

Python 中的机器学习预处理

查看课程

练习说明

  • filtered_text 文本向量与 y 标签(即 category_desc 标签)使用 train_test_split(),并将 y 传给 stratify 参数,因为类别分布不均衡。
  • nb 朴素贝叶斯模型拟合到 X_trainy_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.____)
编辑并运行代码