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

标签 r performance r-caret

我正在构建一个 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/

相关文章:

php - 在 PHP 中向排序数组中插入元素有哪些更好的方法

r - caret::train:为 mlpWeightDecay(RSNNS 包)指定更多非调整参数

r - 训练 H2O 模型时忽略 ID 变量

r - ggplot2:在绘图和图例中用白色填充的线+点?

r - 在 Windows 10 上安装 R 包的问题

r - parLapply 在 R6 类别内

performance - 慈善机构如何衡量捐赠的 CPU 使用率?

java - 使用一致性缓存的开销

r - 时间序列 - 数据分割和模型评估

随机选择 10% 的训练集进行插入符 R 中的交叉验证