首页 > 解决方案 > 网格搜索后如何从 XGBRegressor 函数中提取参数?

问题描述

我是 XGBoost 和参数调整的新手,但我希望你能帮助我。

我正在关注本教程:

https://www.datacamp.com/community/tutorials/xgboost-in-python

它提到尝试实现网格搜索来微调交叉验证部分附近的超参数。

我能够使用 GridSearchCV 返回一组 best_estimator 参数,如下所示:

XGBRegressor(alpha=5, base_score=0.5, booster='gbtree', colsample_bylevel=1,
       colsample_bynode=1, colsample_bytree=0.4, gamma=0,
       importance_type='gain', learning_rate=0.1, max_delta_step=0,
       max_depth=5, min_child_weight=1, missing=None, n_estimators=50,
       n_jobs=1, nthread=None, objective='reg:squarederror',
       random_state=123, reg_alpha=0, reg_lambda=1, scale_pos_weight=1,
       seed=None, silent=None, subsample=1, verbosity=1)

我的问题是,如果我想将其中一些现在调整的参数传递到下一个交叉验证步骤,我如何从上面的列表中获取以下函数的适当值?:

params = {"objective":"reg:squarederror",'colsample_bytree': **THE COLSAMPLE_BYTREE VALUE FROM ABOVE**,'learning_rate': 0.1,
                'max_depth': **THE MAX_DEPTH VALUE FROM ABOVE**, 'alpha': **THE ALPHA VALUE FROM ABOVE**}

cv_results = xgb.cv(dtrain=data_dmatrix, params=params, nfold=3,
                    num_boost_round=50,early_stopping_rounds=10,metrics="rmse", as_pandas=True, seed=123)

...或者这是一个愚蠢的问题?只是试图使用波士顿多元住房数据集来掌握其中一些新技能。如果您想查看我的代码,请告诉我,但希望这样就足够了吗?谢谢!

标签: pythonpython-3.xxgboostgrid-search

解决方案


在我看来,你不需要 best_estimator 来完成这项任务。您可以使用例如 best_params 或 best_index 指令来获取有关您感兴趣的参数的信息。


best_params_ : dict 
Parameter setting that gave the best results on the hold out data.

For multi-metric evaluation, this is present only if refit is specified.

best_index_ : int
The index (of the cv_results_ arrays) which corresponds to the best candidate parameter setting.

The dict at search.cv_results_['params'][search.best_index_] gives the parameter setting for the best model, that gives the highest mean score (search.best_score_).

For multi-metric evaluation, this is present only if refit is specified.

网格搜索文档

然后您可以将此参数视为字典并将键/值放在代码的适当位置,例如


your_best_res = cv_.best_params

'max_depth': your_best_res.max_depth, ...


推荐阅读