使用 GBM 的情感分析
现在让我们在 reviews 数据集上使用 scikit-learn 的 GradientBoostingClassifier,根据评论文本来预测其情感。
我们不会将原始文本直接作为模型输入。以下预处理步骤已为您完成:
- 移除存在缺失值的评论。
- 仅选择排名前 5 个应用的数据。
- 随机抽取 500 条评论的子样本。
- 从评论中移除 "stop words"(停用词)。
- 将评论转换为矩阵,其中每个特征表示某个词在一条评论中的出现频率。
想更深入了解文本挖掘吗?欢迎学习课程《Python 自然语言处理入门》:Introduction to Natural Language Processing in Python!
本练习是课程的一部分
Python 中的集成方法
练习说明
- 构建一个
GradientBoostingClassifier,设置100个基学习器,学习率为0.1。 - 在测试集上计算预测结果。
- 计算准确率以评估模型。
- 计算并打印混淆矩阵。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Build and fit a Gradient Boosting classifier
clf_gbm = ____(____, ____, random_state=500)
clf_gbm.fit(X_train, y_train)
# Calculate the predictions on the test set
pred = ____
# Evaluate the performance based on the accuracy
acc = ____
print('Accuracy: {:.3f}'.format(acc))
# Get and show the Confusion Matrix
cm = ____
print(cm)