使用网格搜索调整了3个参数,但best_estimator_只有2个参数



我正在使用管道和网格搜索调优梯度增强分类器

我的管道是

pipe = make_pipeline(StandardScaler(with_std=True, with_mean=True), 
RFE(RandomForestClassifier(), n_features_to_select= 15), 
GradientBoostingClassifier(random_state=42, verbose=True))

参数gri为:

tuned_parameters = [{'gradientboostingclassifier__max_depth': range(3, 5),
'gradientboostingclassifier__min_samples_split': range(4,6),
'gradientboostingclassifier__learning_rate':np.linspace(0.1, 1, 10)}]

网格搜索以

方式完成
grid = GridSearchCV(pipe, tuned_parameters, cv=5, scoring='accuracy', refit=True)
grid.fit(X_train, y_train)

在列车数据中拟合模型后,当我检查grid.best_estimator时,我只能找到我拟合的2个参数(learning_rate and min_samples_split)。我没有在最佳估计量中找到max_depth参数。

grid.best_estimator_.named_steps['gradientboostingclassifier'] =
GradientBoostingClassifier(learning_rate=0.9, min_samples_split=5,
random_state=42, verbose=True)

但是,如果我使用grid.cv_results来找到最好的'mean_test_score'并找到该测试分数的相应参数,那么我可以在其中找到max_depth

inde = np.where(grid.cv_results_['mean_test_score'] == max(grid.cv_results_['mean_test_score']))
grid.cv_results_['params'][inde[-1][0]]
{'gradientboostingclas...rning_rate': 0.9, 'gradientboostingclas..._max_depth': 3, 'gradientboostingclas...ples_split': 5}
special variables
function variables
'gradientboostingclassifier__learning_rate':0.9
'gradientboostingclassifier__max_depth':3
'gradientboostingclassifier__min_samples_split':5

我现在的疑问是,如果我使用训练有素的管道(对象的名称是'grid'在我的情况下),它还会使用'max_depth'参数吗?那么使用'best parameters'是不是更好,它给了我最好的'mean_test_score',取自grid.cv_results

您的管道已经根据您指定的所有三个参数进行了调优。只是max_depth的最佳值恰好是默认值。在打印分类器时,将不包括默认值。比较以下输出:

print(GradientBoostingClassifier(max_depth=3)) # default
# output: GradientBoostingClassifier()
print(GradientBoostingClassifier(max_depth=5)) # not default
# output: GradientBoostingClassifier(max_depth=5)

一般来说,最佳实践是通过拟合的GridSearchCV对象的best_params_属性访问最佳参数,因为这将始终包括所有参数:

grid.best_params_

我在查看GridSearchCV的结果时遇到了类似的问题

我使用的代码

xgb_regressor = xgboost.XGBRegressor()
parameters_grid = {
'max_depth': [4,8,9],
'learning_rate': [0.01,0.1,0.2],
'gamma': [0.1,0.25, 0.5, 0.7],
'min_child_weight': [ 4, 5,6,7],
'reg_lambda': [0.1, 0.3,0.5, 1, 5],
'n_estimators': [60,70,80,90]
}
grid_search = GridSearchCV(
estimator=xgb_regressor,
param_grid=parameters_grid,
n_jobs = 10,
cv = 10,
verbose=True
)
grid_search.fit(X_train, y_train)
grid_search.best_estimator_

它给了我一个没有reg_lambda的长列表(注释有"…")

XGBRegressor(base_score=None, booster=None, callbacks=None,
colsample_bylevel=None, colsample_bynode=None,
colsample_bytree=None, early_stopping_rounds=None,
enable_categorical=False, eval_metric=None, feature_types=None,
gamma=0.7, gpu_id=None, grow_policy=None, importance_type=None,
interaction_constraints=None, learning_rate=0.2, max_bin=None,
max_cat_threshold=None, max_cat_to_onehot=None,
max_delta_step=None, max_depth=9, max_leaves=None,
min_child_weight=4, missing=nan, monotone_constraints=None,
n_estimators=90, n_jobs=None, num_parallel_tree=None,
predictor=None, random_state=None, ...)

我找到了查看不显示

的值的方法
grid_search.best_estimator_.reg_lambda

打印我正在寻找的调优值。

最新更新