Georg Heiler Georg Heiler - 2 months ago 33
Python Question

Xgboost crossvalidated model access

Is there any way I can access the trained

xgboost
model from
xgboost.cv
directly? Or do I manually have to loop over the folds and perform a fit in this case?

xgb.cv(param, dtrain, num_round, nfold = 5, seed = 0,
obj = logregobj, feval=evalerror)

Answer

First, you cross-validate xgboost as you indicate:

xgb.cv_m <- xgb.cv(param, dtrain, num_round, nfold = 5, seed = 0, obj = logregobj, feval=evalerror)

Then, the number of rounds needed corresponds to the best AUC (the AUC train and test means and std resulted from cross-validation are saved in the data frame 'dt' - check out names(xgb_cv_m)):

nr <- which(xgb_cv_m$dt$test.auc.mean == max(xgb_cv_m$dt$test.auc.mean))

Next, when you fit the final model using 'xgboost' you use nrounds=nr

If you wish, you can also visually inspect the performance at every round by: plot(xgb_cv_m$dt$test.auc.mean)