使用MLR包调整rpart中的parms?

时间:2017-07-20 10:56:00

标签: r rpart mlr

我正在尝试使用MLR包来调整使用rpart包构建的决策树的超参数。即使我可以调整决策树的基本参数(例如minsplitmaxdepth等),我也无法正确设置参数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

我还尝试将parms定义为UntypedParam

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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

你必须像这样定义参数"parms"

makeDiscreteParam("parms", values = list(a = list(prior = c(.6, .4)), b = list(prior = c(.5, .5))))

ab可以是任意名称,仅反映实际值所说的内容。