0

我刚刚完成了基于树的建模的 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 附件,但最终出错。本质上,我需要新列中的每个参数,如下所示。

mean | std   | criterion | ..... | splitter
0.57   0.009    'gini'     ..... | 'best'
0.58   0.029    'entropy'     ..... | 'random'
.
.
.
.
4

1 回答 1

0

您可以使用预制类生成带有参数报告的 DataFrame (请参阅此处使用此代码的 stackoverflow 帖子)

导入和设置

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)

生成一些数据

iris = load_iris()
X_iris = iris.data
y_iris = iris.target

定义模型和超参数网格

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
于 2019-03-06T20:05:11.350 回答