使用 Scikit Learn 的 GridSearchCV
Scikit Learn 的 GridSearchCV 模块提供了许多实用功能,能高效执行网格搜索。现在请将所学付诸实践,根据给定参数创建一个 GridSearchCV 对象。
需要的配置如下:
- 随机森林估计器,划分准则为 'entropy'
- 5 折交叉验证
- 超参数
max_depth(2、4、8、15)与max_features('auto' 与 'sqrt') - 使用
roc_auc作为模型评分指标 - 并行使用 4 个核心进行处理
- 需要对最佳模型进行重新拟合,并返回训练集评分
您将可使用 X_train、X_test、y_train 与 y_test 数据集。
本练习是课程的一部分
Python 中的超参数调优
练习说明
- 按上述背景说明创建一个随机森林估计器。
- 按上述背景说明创建参数网格。
- 使用前两步创建的对象,按上述背景说明创建一个
GridSearchCV对象。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create a Random Forest Classifier with specified criterion
rf_class = RandomForestClassifier(____=____)
# Create the parameter grid
param_grid = {____: ____, ____: ____}
# Create a GridSearchCV object
grid_rf_class = GridSearchCV(
estimator=____,
param_grid=____,
scoring=____,
n_jobs=____,
cv=____,
refit=____, return_train_score=____)
print(grid_rf_class)