实现 GridSearch
现在,您可以使用 sklearn 的 GridSearchCV() 函数,来搜索上一练习中生成的所有 max_depth 和 min_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.____)