def tune_num_estimators(metric: str,
label: np.ndarray,
params: dict,
strat_folds: StratifiedKFold,
train) -> Tuple[int, float]:
"""
Uses xgboost's cross-validation method to tune the number of estimators and returns that along with the best CV score
achieved.
:param metric:
Evaluation metric that is monitored during cross-validation - e.g. 'logloss' or 'rmse'.
:param label:
An array-like containing the labels of the classification or regression problem.
:param params:
A dictionary of XGB parameters.
:param strat_folds:
A StratifiedKFold object to cross validate the parameters.
:param train:
An array-like containing the training input samples.
:return:
A tuple containing the tuned number of estimators along with the best CV score achieved.
"""
eval_hist = xgb.cv(
dtrain=xgb.DMatrix(train, label=label),
early_stopping_rounds=50,
folds=strat_folds,
metrics=metric,
num_boost_round=10000,
params=params,
verbose_eval=True
)
num_trees = eval_hist.shape[0]
best_score = eval_hist.values[num_trees - 1, 0]
return num_trees, best_score
评论列表
文章目录