以下是我在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()中的错误: 在&#39; params&#39;中设定目标和&#39; obj&#39;同时不允许
让我知道我的代码中有什么问题。
感谢任何帮助。
此致
莫汉
答案 0 :(得分:5)
问题是由于watchlist
参数传递给xgboost
watchlist
是[{1}}的参数,但不是xgb.train
的参数,因此xgboost
会将其视为&#34;其他参数&#34; (xgboost
)。
以下代码
...
正常工作
xgMod <- xgboost(param = param, data = train_gl, nrounds = nrounds)