使用 tf/idf 向量进行文本分类
现在,您已经将 volunteer 数据集的 title 列编码为 tf/idf 向量,接下来将使用这些向量来预测 category_desc 列。
本练习是课程的一部分
Python 中的机器学习预处理
练习说明
- 将
text_tfidf向量与目标变量y划分为训练集和测试集。由于类别分布不均,请将stratify参数设为y。注意,我们需要对 tf/idf 向量调用.toarray()方法,才能转换为 scikit-learn 需要的格式。 - 将
X_train和y_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.____(____, ____))