2

我正在尝试使用 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

我也尝试定义parmsUntypedParamwith

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

有人可以帮忙吗?

4

1 回答 1

2

您必须"parms"像这样定义参数:

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

a并且b可以是仅反映实际值的任意名称。

于 2017-07-20T12:26:08.760 回答