开始使用免费开始使用

Bagging:scikit-learn 实现

现在让我们把 scikit-learnBaggingClassifier 用到 Pokémon 数据集上。

您用自定义的 bagging 集成得到的 F1 分数大约是 0.63

BaggingClassifier() 能超过它吗?现在就来验证!

本练习是课程的一部分

Python 中的集成方法

查看课程

练习说明

  • 实例化基础模型 clf_dt:一个"受限"的决策树,最大深度为 4。
  • 以该决策树为基础学习器,构建包含 21 个基学习器的 bagging 分类器。
  • 预测测试集的标签。

交互式实操练习

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

# Instantiate the base model
clf_dt = ____

# Build the Bagging classifier
clf_bag = ____(____, ____, random_state=500)

# Fit the Bagging model to the training set
clf_bag.fit(X_train, y_train)

# Predict the labels of the test set
pred = ____

# Show the F1-score
print('F1-Score: {:.3f}'.format(f1_score(y_test, pred)))
编辑并运行代码