Search code examples
rxgboostgbm

use customized error metrics in xgb.cv -- how to call object 'pred' inside xgb.cv(R not Python)


I'm trying to use a customized error metrics in xgb.cv, below is the error metrics code:

evalerror <- function(preds, dtrain) {
  labels <- getinfo(dtrain, "label")
  err <- mape(as.numeric(labels),as.numeric(pred))
  return(list(metric = "mape", value = err))
}

And inside xgb.cv I used prediction = TRUE and feval = evalerror; however I get an error message of cannot find pred each time. I know that prediction = TRUE only stores the prediction of THE LAST cross-validation model in the output list(for example, it can be called using output_xgb.cv$pred) instead of an independent object. Is there any way to call the prediction of EACH cross-validation and use it in the evalerror function?

Thanks!


Solution

  • Figured it out. Turned out there was a typo in the code, should be as.numeric(preds).