R, xgboost: label must be in [0,1] for logistic regression

試著忘記壹切 提交于 2019-12-11 14:03:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!