从gridsearch提取结果

2024-09-27 07:21:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚完成了一个基于树的建模的gridsearch CV,在查看结果之后,我设法从gridsearchCV中访问每个迭代的结果。在

我需要每个运行到一个单独的行和每个参数在一个单独的列。 我可以对每一行运行一个循环或列表理解,但无法将每个运行分隔成列

 df = grid.grid_scores_
 df[0]
 mean: 0.57114, std: 0.00907, params: {'criterion': 'gini', 'max_depth': 10, 
 'max_features': 8, 'min_samples_leaf': 2, 'min_samples_split': 2, 'splitter': 'best'}`

我尝试使用tuple和dict附件,但最终出现了错误。基本上,我需要一个新列中的每个参数,如下所示。在

^{pr2}$

Tags: df列表参数minmean建模cvmax
1条回答
网友
1楼 · 发布于 2024-09-27 07:21:19

您可以使用预先生成的class to generate a DataFrame with a report of the parameters(请参阅stackoverflow post using this codehere

导入和settings

import pandas as pd
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from gridsearchcv_helper import EstimatorSelectionHelper
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

生成一些数据

^{pr2}$

定义模型和超参数网格

models = {'RandomForestClassifier': RandomForestClassifier()}
params = {'RandomForestClassifier': { 'n_estimators': [16, 32],
                                      'max_features': ['auto', 'sqrt', 'log2'],
                                      'criterion' : ['gini', 'entropy'] }}

执行网格搜索(使用CV)并报告结果

helper = EstimatorSelectionHelper(models, params)
helper.fit(X_iris, y_iris, n_jobs=4)
df_gridsearchcv_summary = helper.score_summary()

这是输出

print(type(df_gridsearchcv_summary))
print(df_gridsearchcv_summary.iloc[:,1:])

RandomForestClassifier
<class 'pandas.core.frame.DataFrame'>
   min_score mean_score max_score  std_score criterion max_features n_estimators
0   0.941176   0.973856         1  0.0244553      gini         auto           16
1   0.921569    0.96732         1  0.0333269      gini         auto           32
8   0.921569    0.96732         1  0.0333269   entropy         sqrt           16
10  0.921569    0.96732         1  0.0333269   entropy         log2           16
2   0.941176   0.966912  0.980392  0.0182045      gini         sqrt           16
3   0.941176   0.966912  0.980392  0.0182045      gini         sqrt           32
4   0.941176   0.966912  0.980392  0.0182045      gini         log2           16
5   0.901961   0.960784         1  0.0423578      gini         log2           32
6   0.921569   0.960376  0.980392  0.0274454   entropy         auto           16
7   0.921569   0.960376  0.980392  0.0274454   entropy         auto           32
11  0.901961    0.95384  0.980392  0.0366875   entropy         log2           32
9   0.921569   0.953431  0.980392  0.0242635   entropy         sqrt           32

相关问题 更多 >

    热门问题