开始使用免费开始使用

实现 GridSearch

现在,您可以使用 sklearnGridSearchCV() 函数,来搜索上一练习中生成的所有 max_depthmin_samples_leaf 取值组合,从而找到最佳组合。

本练习是课程的一部分

HR Analytics:用 Python 预测员工流失

查看课程

练习说明

  • 导入 GridSearchCV 函数。
  • 使用您之前定义的 parameters 字典,对 model 应用 GridSearchCV(),并将结果保存为 param_search
  • param_search 拟合到训练数据集。
  • 使用 best_params_ 属性打印找到的最佳参数。

交互式实操练习

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

# import the GridSearchCV function
from sklearn.model_selection import ____

# set up parameters: done
parameters = dict(max_depth=depth, min_samples_leaf=samples)

# initialize the param_search function using the GridSearchCV function, initial model and parameters above
param_search = ____(model, parameters, cv=3)

# fit the param_search to the training dataset
____.fit(features_train, target_train)

# print the best parameters found
print(param_search.____)
编辑并运行代码