我正在构建一个 CART 模型,并尝试调整 rpart 的 2 个参数 - CP 和 Max深度。虽然 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
最佳答案
caret
无法使用集成方法做到这一点,因此您必须添加自己的方法。
或者,您可以在 mlr
上尝试此操作,这是一个类似的机器学习框架,它允许开箱即用的许多重采样策略、调整控制方法和算法参数调整。有很多learners already implemented ,有几个不同的evaluation metrics to choose from .
针对您的具体问题,请尝试以下示例:
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
具有令人难以置信的多功能性:包装器方法允许人们将学习者与调优策略、预处理、过滤和插补步骤等融合在一起。
关于r - 如何使用 Caret 包调整多个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36802846/