开始使用免费开始使用

探索网格搜索结果

现在,您将探索视频中定义的 GridSearchCV 对象的 cv_results_ 属性。它是一个字典,我们可以将其读入 pandas 的 DataFrame,并包含我们刚刚执行的网格搜索的诸多有用信息。

对此属性中不同列类型的提醒:

  • time_
  • param_ 列(每个超参数一个)以及那个单独的 params 列(包含所有超参数设置)
  • 每个交叉验证折都有一个 train_score 列,此外还有 mean_train_scorestd_train_score
  • 每个交叉验证折都有一个 test_score 列,此外还有 mean_test_scorestd_test_score
  • rank_test_score 列,其取值为 1 到 n(迭代次数),根据各自的 mean_test_score 对行进行排名

本练习是课程的一部分

Python 中的超参数调优

查看课程

练习说明

  • grid_rf_class 这个 GridSearchCV 对象的 cv_results_ 属性读入一个数据框,并打印整个数据框以便检查。
  • 提取并打印那个"单独"的列,其中包含每次网格搜索迭代所用全部超参数的字典。
  • 使用 rank_test_score 列进行索引,提取并打印具有最佳平均测试分数的那一行。

交互式实操练习

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

# Read the cv_results property into a dataframe & print it out
cv_results_df = pd.DataFrame(grid_rf_class.____)
print(____)

# Extract and print the column with a dictionary of hyperparameters used
column = cv_results_df.loc[:, [____]]
print(____)

# Extract and print the row that had the best mean test score
best_row = cv_results_df[cv_results_df[____] == ____ ]
print(best_row)
编辑并运行代码