如何使用Caret包调整多个参数?

7

我正在构建一个CART模型,试图调整rpart的两个参数 - CP和Maxdepth。虽然Caret包可以单独调整一个参数,但当两个参数一起使用时,它会不断地出现错误。我无法弄清楚原因。

library(caret)
data(iris)
tc <- trainControl("cv",10)
rpart.grid <- expand.grid(cp=seq(0,0.1,0.01), minsplit=c(10,20)) 
train(Petal.Width ~ Petal.Length + Sepal.Width + Sepal.Length, data=iris, method="rpart", 
      trControl=tc,  tuneGrid=rpart.grid)

我收到了以下错误信息:
Error in train.default(x, y, weights = w, ...) : 
  The tuning parameter grid should have columns cp
2个回答

6
caret无法使用集成方法完成此操作,因此您需要添加自己的方法。或者,您可以尝试在类似的机器学习框架mlr上进行操作,它允许许多重采样策略、调整控制方法和算法参数调整。已经实现了许多学习器,提供了几种不同的评估指标供选择。针对您的具体问题,请尝试此示例:
library(mlr)
iris.task = classif.task = makeClassifTask(id = "iris-example", data = iris, target = "Species")
resamp = makeResampleDesc("CV", iters = 10L)

lrn = makeLearner("classif.rpart")

control.grid = makeTuneControlGrid() 
#you can pass resolution = N if you want the algorithm to 
#select N tune params given upper and lower bounds to a NumericParam
#instead of a discrete one
ps = makeParamSet(
  makeDiscreteParam("cp", values = seq(0,0.1,0.01)),
  makeDiscreteParam("minsplit", values = c(10,20))
)

#you can also check all the tunable params
getParamSet(lrn)

#and the actual tuning, with accuracy as evaluation metric
res = tuneParams(lrn, task = iris.task, resampling = resamp, control = control.grid, par.set = ps, measures = list(acc,timetrain))
opt.grid = as.data.frame(res$opt.path)
print(opt.grid)

mlr 非常灵活:包装器方法允许将学习器与调优策略、预处理、过滤和插补步骤等融合在一起,还有更多功能。


1
我正在使用mlr的初步阶段,也许在tuneParams函数中应该使用学习器lrn - Enrique Pérez Herrero
1
@EnriquePérezHerrero 您说得完全正确,感谢您的提醒! - catastrophic-failure

5

“rpart”方法只能调整cp参数,“rpart2”方法用于maxdepth。minsplit或其他rpart控制的任何调整都不可行。如果您想在不同选项上进行调整,可以编写自定义模型来考虑这一点。

点击此处了解更多信息。还可以阅读此答案,了解如何在train函数中使用rpart控件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接