分析最佳结果
归根结底,我们最关心的是网格搜索中表现最好的那个"方格"。幸运的是,Scikit Learn 的 gridSearchCv 对象提供了若干属性,可直接给出关于最佳方格(或 cv_results_ 中那一行)的关键信息。
您将要查看的三个属性是:
best_score_—— 最佳方格对应的分数(此处为 ROC_AUC)。best_index_——cv_results_中包含最佳方格信息的那一行的索引。best_params_—— 产生最佳分数的参数字典,例如'max_depth': 10。
网格搜索对象 grid_rf_class 已可用。
第 6 行已基于 cv_results_ 为您创建了一个数据框(cv_results_df)。这将帮助您按索引访问结果。
本练习是课程的一部分
Python 中的超参数调优
练习说明
- 从
grid_rf_class中最佳方格提取并打印 ROC_AUC 分数。 - 通过对
cv_results_df进行索引,从最佳行创建一个变量。 - 通过从
grid_rf_class的最佳方格中提取n_estimators参数,创建变量best_n_estimators,并将其打印出来。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Print out the ROC_AUC score from the best-performing square
best_score = grid_rf_class._____
print(best_score)
# Create a variable from the row related to the best-performing square
cv_results_df = pd.DataFrame(grid_rf_class.cv_results_)
best_row = cv_results_df.loc[[grid_rf_class.____]]
print(best_row)
# Get the n_estimators parameter from the best-performing square and print
best_n_estimators = grid_rf_class.____["_____"]
print(best_n_estimators)