了解 R 中 xgboost 的 num_classes [英] Understanding num_classes for xgboost in R
问题描述
我在弄清楚如何正确设置 xgboost 的 num_classes 时遇到了很多麻烦.
I'm having a lot of trouble figuring out how to correctly set the num_classes for xgboost.
我有一个使用 Iris 数据的例子
I've got an example using the Iris data
df <- iris
y <- df$Species
num.class = length(levels(y))
levels(y) = 1:num.class
head(y)
df <- df[,1:4]
y <- as.matrix(y)
df <- as.matrix(df)
param <- list("objective" = "multi:softprob",
"num_class" = 3,
"eval_metric" = "mlogloss",
"nthread" = 8,
"max_depth" = 16,
"eta" = 0.3,
"gamma" = 0,
"subsample" = 1,
"colsample_bytree" = 1,
"min_child_weight" = 12)
model <- xgboost(param=param, data=df, label=y, nrounds=20)
这会返回一个错误
Error in xgb.iter.update(bst$handle, dtrain, i - 1, obj) :
SoftmaxMultiClassObj: label must be in [0, num_class), num_class=3 but found 3 in label
如果我将 num_class 更改为 2,我会得到同样的错误.如果我将 num_class 增加到 4,那么模型会运行,但我会得到 600 个预测概率,这对于 4 个类是有意义的.
If I change the num_class to 2 I get the same error. If I increase the num_class to 4 then the model runs, but I get 600 predicted probabilities back, which makes sense for 4 classes.
我不确定我是否犯了错误,或者我是否没有理解 xgboost 的工作原理.任何帮助,将不胜感激.
I'm not sure if I'm making an error or whether I'm failing to understand how xgboost works. Any help would be appreciated.
推荐答案
label must be in [0, num_class)在您的脚本中,在 model <-...
label must be in [0, num_class)
in your script add y<-y-1
before model <-...
这篇关于了解 R 中 xgboost 的 num_classes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!