开始使用免费开始使用

使用 tf/idf 向量进行文本分类

现在,您已经将 volunteer 数据集的 title 列编码为 tf/idf 向量,接下来将使用这些向量来预测 category_desc 列。

本练习是课程的一部分

Python 中的机器学习预处理

查看课程

练习说明

  • text_tfidf 向量与目标变量 y 划分为训练集和测试集。由于类别分布不均,请将 stratify 参数设为 y注意,我们需要对 tf/idf 向量调用 .toarray() 方法,才能转换为 scikit-learn 需要的格式。
  • X_trainy_train 拟合到朴素贝叶斯模型 nb
  • 打印测试集的准确率。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Split the dataset according to the class distribution of category_desc
y = volunteer["category_desc"]
X_train, X_test, y_train, y_test = ____(____.toarray(), ____, ____=____, random_state=42)

# Fit the model to the training data
nb.____(____, ____)

# Print out the model's accuracy
print(nb.____(____, ____))
编辑并运行代码