探索网格搜索结果
现在,您将探索视频中定义的 GridSearchCV 对象的 cv_results_ 属性。它是一个字典,我们可以将其读入 pandas 的 DataFrame,并包含我们刚刚执行的网格搜索的诸多有用信息。
对此属性中不同列类型的提醒:
time_列param_列(每个超参数一个)以及那个单独的params列(包含所有超参数设置)- 每个交叉验证折都有一个
train_score列,此外还有mean_train_score和std_train_score列 - 每个交叉验证折都有一个
test_score列,此外还有mean_test_score和std_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)