r语言 - 错误:参数"x"丢失,没有默认值?



作为XGBoost的新手,我正试图使用mlr库和模型来调整参数,但在使用setHayperPars((后,使用train((学习会引发错误(尤其是当我运行xgmodel行时(:colnames(x(中的错误:缺少参数"x",没有默认值,我无法识别这个错误的含义,下面是代码:

library(mlr)     
library(dplyr)
library(caret) 
library(xgboost)
set.seed(12345)
n=dim(mydata)[1]
id=sample(1:n, floor(n*0.6)) 
train=mydata[id,]
test=mydata[-id,]
traintask = makeClassifTask (data = train,target = "label")
testtask = makeClassifTask (data = test,target = "label")
#create learner
lrn = makeLearner("classif.xgboost",
predict.type = "response")
lrn$par.vals = list( objective="multi:softprob",
eval_metric="merror")
#set parameter space
params = makeParamSet( makeIntegerParam("max_depth",lower = 3L,upper = 10L),
makeIntegerParam("nrounds",lower = 20L,upper = 100L),
makeNumericParam("eta",lower = 0.1, upper = 0.3),
makeNumericParam("min_child_weight",lower = 1L,upper = 10L), 
makeNumericParam("subsample",lower = 0.5,upper = 1), 
makeNumericParam("colsample_bytree",lower = 0.5,upper = 1)) 

#set resampling strategy
configureMlr(show.learner.output = FALSE, show.info = FALSE)
rdesc = makeResampleDesc("CV",stratify = T,iters=5L)
# set the search optimization strategy
ctrl = makeTuneControlRandom(maxit = 10L)
# parameter tuning
set.seed(12345)
mytune = tuneParams(learner = lrn, task = traintask, 
resampling = rdesc, measures = acc, 
par.set = params, control = ctrl,
show.info = FALSE)

# build model using the tuned paramters 
#set hyperparameters
lrn_tune = setHyperPars(lrn,par.vals = mytune$x)
#train model
xgmodel = train(learner = lrn_tune,task = traintask)

有人能告诉我怎么了吗!?

当加载多个可能涉及同名方法的包时,必须非常小心,这里是caretmlr,它们都包括train方法。此外,library语句的顺序是重要的:这里,由于caret是在mlr之后加载的,它屏蔽了与它同名的函数(可能还有之前加载的所有其他包(,如train

在您的情况下,如果您显然希望使用mlr(而不是caret(中的train方法,您应该在代码中明确声明:

xgmodel = mlr::train(learner = lrn_tune,task = traintask)

相关内容

  • 没有找到相关文章

最新更新