R 中的 xgboost() 函数出错(不允许同时在 'params' 和 'obj' 中设置目标)

Error (Setting objectives in 'params' and 'obj' at the same time is not allowed) in xgboost() function in R

下面是我在 XGBOOST 上执行的代码,

data(Glass, package = "mlbench")

levels(Glass$Type) <- c(0:5) #Proper Sequence. Should start with 0

Glass$Type <- as.integer(as.character(Glass$Type))

set.seed(100)
options(scipen = 999)


library(caret)
R_index <- createDataPartition(Glass$Type, p=.7, list = FALSE)
gl_train <- Glass[R_index,]
gl_test <- Glass[-R_index,]


'%ni%' <- Negate('%in%')

library(xgboost)
library(Matrix)

#Creating the matrix for training the model
train_gl <- xgb.DMatrix(data.matrix(gl_train[ ,colnames(gl_train) %ni% 'Type']),
                        label = as.numeric(gl_train$Type))

test_gl <- xgb.DMatrix(data.matrix(gl_test[ ,colnames(gl_test) %ni% 'Type']))

watchlist <- list(train = gl_train, test = gl_test)


#Define the parameters and cross validate
param <- list("objective" = "multi:softmax",
              "eval_metric" = "mlogloss",
              "num_class" = length(unique(gl_train$Type)))




cv.nround <- 5
cv.nfold <- 3

cvMod <- xgb.cv(param = param, data = train_gl,
                nfold = cv.nfold,
                nrounds = cv.nround,
                watchlist=watchlist)


#Build the Model
nrounds = 50
xgMod = xgboost(param = param, data = train_gl, nrounds = nrounds, watchlist = watchlist)

执行 xgMod 后出现以下错误,

check.custom.obj() 中的错误: 不允许同时在 'params' 和 'obj' 中设置目标

让我知道我的代码有什么问题。

感谢任何帮助。

此致,

墨涵

问题是由于 watchlist 参数传递给 xgboost
watchlistxgb.train 的参数而不是 xgboost 的参数,因此 xgboost 认为它像 "other parameters" (...) .

如下代码

xgMod <- xgboost(param = param, data = train_gl, nrounds = nrounds)

工作正常

[1]     train-mlogloss:1.259886 
[2]     train-mlogloss:0.963367 
[3]     train-mlogloss:0.755535 
[4]     train-mlogloss:0.601647 
[5]     train-mlogloss:0.478923
...