问题
I am getting an error saying that my label must be in [0, 1]:
> system.time(xgb <- xgboost(params = param,
+ data = dtrain,
+ label = as.numeric(train.label),
+ nrounds = 500,
+ print_every_n = 100,
+ verbose = 1))
Error in xgb.iter.update(bst$handle, dtrain, iteration - 1, obj) :
[10:39:29] amalgamation/../src/objective/regression_obj.cc:108: label must be in [0,1] for logistic regression
Timing stopped at: 0.11 0 0.11
However, my label is in [0, 1]:
> train.label
[1] 1 1 1 1 1 0 0 1 1 0 0 1 1 1 1 0 1 1 1 1 1 0 1 1 . . .
[ reached getOption("max.print") -- omitted 38907 entries ]
Levels: 0 1
I also tried converting to a numeric
data type without luck.
回答1:
The problem is that your train.label
is a factor, so your code
as.numeric(train.label)
will produce a vector containing 1's and 2's. You want the 0's and 1's as the values, so you need to use.
as.numeric(as.character(train.label))
来源:https://stackoverflow.com/questions/48999589/r-xgboost-label-must-be-in-0-1-for-logistic-regression