Skip to contents

Fuses a base learner with a search strategy to select its hyperparameters. Creates a learner object, which can be used like any other learner object, but which internally uses tuneParams. If the train function is called on it, the search strategy and resampling are invoked to select an optimal set of hyperparameter values. Finally, a model is fitted on the complete training data with these optimal hyperparameters and returned. See tuneParams for more details.

After training, the optimal hyperparameters (and other related information) can be retrieved with getTuneResult.

Usage

makeTuneWrapper(
  learner,
  resampling,
  measures,
  par.set,
  control,
  show.info = getMlrOption("show.info")
)

Arguments

learner

(Learner | character(1))
The learner. If you pass a string the learner will be created via makeLearner.

resampling

(ResampleInstance | ResampleDesc)
Resampling strategy to evaluate points in hyperparameter space. If you pass a description, it is instantiated once at the beginning by default, so all points are evaluated on the same training/test sets. If you want to change that behavior, look at TuneControl.

measures

(list of Measure | Measure)
Performance measures to evaluate. The first measure, aggregated by the first aggregation function is optimized, others are simply evaluated. Default is the default measure for the task, see here getDefaultMeasure.

par.set

(ParamHelpers::ParamSet)
Collection of parameters and their constraints for optimization. Dependent parameters with a requires field must use quote and not expression to define it.

control

(TuneControl)
Control object for search method. Also selects the optimization algorithm for tuning.

show.info

(logical(1))
Print verbose output on console? Default is set via configureMlr.

Value

Learner.

Examples

# \donttest{
task = makeClassifTask(data = iris, target = "Species")
lrn = makeLearner("classif.rpart")
# stupid mini grid
ps = makeParamSet(
  makeDiscreteParam("cp", values = c(0.05, 0.1)),
  makeDiscreteParam("minsplit", values = c(10, 20))
)
ctrl = makeTuneControlGrid()
inner = makeResampleDesc("Holdout")
outer = makeResampleDesc("CV", iters = 2)
lrn = makeTuneWrapper(lrn, resampling = inner, par.set = ps, control = ctrl)
mod = train(lrn, task)
#> Error in x[0, , drop = FALSE]: incorrect number of dimensions
print(getTuneResult(mod))
#> Error in checkClass(x, classes, ordered, null.ok): object 'mod' not found
# nested resampling for evaluation
# we also extract tuned hyper pars in each iteration
r = resample(lrn, task, outer, extract = getTuneResult)
#> Resampling: cross-validation
#> Measures:             mmce      
#> [Tune] Started tuning learner classif.rpart for parameter set:
#>              Type len Def   Constr Req Tunable Trafo
#> cp       discrete   -   - 0.05,0.1   -    TRUE     -
#> minsplit discrete   -   -    10,20   -    TRUE     -
#> With control class: TuneControlGrid
#> Imputation value: 1
#> [Tune-x] 1: cp=0.05; minsplit=10
#> [Tune-y] 1: mmce.test.mean=0.0400000; time: 0.0 min
#> [Tune-x] 2: cp=0.1; minsplit=10
#> [Tune-y] 2: mmce.test.mean=0.0400000; time: 0.0 min
#> [Tune-x] 3: cp=0.05; minsplit=20
#> [Tune-y] 3: mmce.test.mean=0.0400000; time: 0.0 min
#> [Tune-x] 4: cp=0.1; minsplit=20
#> [Tune-y] 4: mmce.test.mean=0.0400000; time: 0.0 min
#> [Tune] Result: cp=0.05; minsplit=20 : mmce.test.mean=0.0400000
#> [Resample] iter 1:    0.0533333 
#> [Tune] Started tuning learner classif.rpart for parameter set:
#>              Type len Def   Constr Req Tunable Trafo
#> cp       discrete   -   - 0.05,0.1   -    TRUE     -
#> minsplit discrete   -   -    10,20   -    TRUE     -
#> With control class: TuneControlGrid
#> Imputation value: 1
#> [Tune-x] 1: cp=0.05; minsplit=10
#> [Tune-y] 1: mmce.test.mean=0.0800000; time: 0.0 min
#> [Tune-x] 2: cp=0.1; minsplit=10
#> [Tune-y] 2: mmce.test.mean=0.0800000; time: 0.0 min
#> [Tune-x] 3: cp=0.05; minsplit=20
#> [Tune-y] 3: mmce.test.mean=0.0800000; time: 0.0 min
#> [Tune-x] 4: cp=0.1; minsplit=20
#> [Tune-y] 4: mmce.test.mean=0.0800000; time: 0.0 min
#> [Tune] Result: cp=0.1; minsplit=10 : mmce.test.mean=0.0800000
#> [Resample] iter 2:    0.0800000 
#> 
#> Aggregated Result: mmce.test.mean=0.0666667
#> 
print(r$extract)
#> [[1]]
#> Tune result:
#> Op. pars: cp=0.05; minsplit=20
#> mmce.test.mean=0.0400000
#> 
#> [[2]]
#> Tune result:
#> Op. pars: cp=0.1; minsplit=10
#> mmce.test.mean=0.0800000
#> 
getNestedTuneResultsOptPathDf(r)
#>     cp minsplit mmce.test.mean dob eol error.message exec.time iter
#> 1 0.05       10           0.04   1  NA          <NA>     0.015    1
#> 2  0.1       10           0.04   2  NA          <NA>     0.014    1
#> 3 0.05       20           0.04   3  NA          <NA>     0.014    1
#> 4  0.1       20           0.04   4  NA          <NA>     0.015    1
#> 5 0.05       10           0.08   1  NA          <NA>     0.016    2
#> 6  0.1       10           0.08   2  NA          <NA>     0.015    2
#> 7 0.05       20           0.08   3  NA          <NA>     0.015    2
#> 8  0.1       20           0.08   4  NA          <NA>     0.014    2
getNestedTuneResultsX(r)
#>     cp minsplit
#> 1 0.05       20
#> 2 0.10       10
# }