How to configure multiple settings with Caret?

I am creating a CART model and I am trying to configure 2 rpart parameters - CP and Maxdepth. Although Caret works well for one parameter at a time when both are used, it continues to throw an error, and I cannot understand why

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)

I get the following error:

Error in train.default(x, y, weights = w, ...) : 
  The tuning parameter grid should have columns cp
+4
source share
2 answers

The rpart method can only configure cp, for maxdepth the rpart2 method is used. There is no setting for minsplit or any other rpart control. If you want to configure various parameters, you can write your own model to take this into account.

, . , rpart .

+4

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 : , , .

+6

All Articles