我正在尝试使用 MLR 包来调整使用 rpart 包构建的决策树的超参数。即使我可以调整决策树的基本参数(例如minsplit
,maxdepth
等(,我无法正确设置参数的值param
。具体来说,我想在网格搜索中尝试不同的priors
。
这里我写的代码(dat
是我正在使用的数据帧,target
是我的类变量(:
# Create a task
dat.task = makeClassifTask(id = "tree", data = dat, target = "target")
# Define the model
resamp = makeResampleDesc("CV", iters = 4L)
# Create the learner
lrn = makeLearner("classif.rpart")
# Create the grid params
control.grid = makeTuneControlGrid()
ps = makeParamSet(
makeDiscreteParam("cp", values = seq(0.001, 0.006, 0.002)),
makeDiscreteParam("minsplit", values = c(1, 5, 10, 50)),
makeDiscreteParam("maxdepth", values = c(20, 30, 50)),
makeDiscreteParam("parms", values = list(prior=list(c(.6, .4),
c(.5, .5))))
)
当我尝试执行调整时,使用:
# Actual tuning, with accuracy as evaluation metric
tuned = tuneParams(lrn, task = dat.task,
resampling = resamp,
control = control.grid,
par.set = ps, measures = acc)
我收到错误
Error in get(paste("rpart", method, sep = "."), envir = environment())(Y, : The parms list must have names
我还试图将UntypedParam
parms
定义为
makeUntypedParam("parms", special.vals = list(prior=list(c(.6, .4), c(.5,.5))))
这是因为通过键入getParamSet("classif.rpart")
,在我看来,调优接受"非类型变量"而不是离散变量。
但是,当我尝试此操作时,出现错误:
Error in makeOptPath(par.set, y.names, minimize, add.transformed.x, include.error.message, :
OptPath can currently only be used for: numeric,integer,numericvector,integervector,logical,logicalvector,discrete,discretevector,character,charactervector
有人可以帮忙吗?
您必须像这样定义参数"parms"
:
makeDiscreteParam("parms", values = list(a = list(prior = c(.6, .4)), b = list(prior = c(.5, .5))))
a
和b
可以是任意名称,仅反映实际值的含义。