2016-04-22 79 views
4

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

回答

4

方法“rpart”只能调整cp,方法“rpart2”用于maxdepth。没有对minsplit或任何其他rpart控件进行调整。如果你想调整不同的选项,你可以编写一个自定义模型来考虑这一点。

点击here了解更多关于如何操作的信息。另请阅读this answer关于如何使用列车功能中的rpart控件。

6

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是令人难以置信的通用:包装方法允许一个融合学习者调整策略,前处理,过滤和估算步骤,等等。

+1

我在使用'mlr'开始我的第一步,也许应该在'tuneParams'函数中使用学习者'lrn'。 –

+1

@EnriquePérezHerrero你绝对正确,谢谢你的支持! –

相关问题