Leonardo Petrini
Leonardo Petrini

Reputation: 11

Xgboost early.stop.round error with custom metric

I'm trying to use "early.stop.round" function of xgb.train (xgboost library - R) on validaiton set, by using a custom evaluation metric (ie "feval" function). However, I get the following error message:

 xgbMatrixTrain <- xgb.DMatrix(as.matrix(train[,-c(1,2)]), label         =train$y,missing="NAN")
 xgbMatrixValid <- xgb.DMatrix(as.matrix(valid[,-c(1,2)]), label = valid$y,      missing="NAN")


MAE <- function(preds, dtrain) {

labels <- getinfo(dtrain, "label")
 err <- as.numeric(sum(abs(exp(labels) - exp(preds))))/length(labels)
  return(list(metric = "mae error", value = err))
}

myWatch <- list(val=xgbMatrixValid,train=xgbMatrixTrain)
 bst.mae2<- xgb.train(params = param.noerr, 
                 data = xgbMatrixTrain, 
                 feval = MAE,                       nround=150, 
                  print.every.n = 1, 
                  watchlist=myWatch, 
                  early.stop.round = 20, 
                 maximize = FALSE)

[0] val-mae error:0.59831651363868  train-mae error:0.598864823842993

Error in if ((maximize && score > bestScore) || (!maximize && score < : missing value where TRUE/FALSE needed

By not using stopping round function I get avoid this error. Any idea on how to solve this, by retaining at the same time the stopping round function?

Thank you all so much! Leo

Upvotes: 1

Views: 1677

Answers (1)

cousin_pete
cousin_pete

Reputation: 578

Without a reproducible example I cannot run your code. The error you get may be nothing to do with your code but may be from errors in your data but hard to say with no data! I have seen error msg like yours when there are missing 0 or 1 values in a binary response variable. Xgboost only handles numeric data so this may be worth a check. Is the missing="NAN" causing problems in your data? What happens if you remove rows with the missing response? HTH, cousin_pete

Upvotes: 1

Related Questions