|
1 | 1 | #' @title Evaluates expressions within a learner or parameter set according to the task. |
2 | 2 | #' |
3 | | -#' @description Updates learners and/or parameter sets by evaluating their expressions |
4 | | -#' based on a specific task. An overview of the possible expressions can be found in the details. |
| 3 | +#' @description |
| 4 | +#' A \code{\link{Learner}} or \code{\link[ParamHelpers]{ParamSet}} can contain an unevaluated \code{\link[base]{expression}} |
| 5 | +#' as value for a hyperparameter. |
| 6 | +#' E.g., these expressions are used if the default value dependents on the task size or an upper limit for a parameter |
| 7 | +#' is given by the number of features in a task. |
| 8 | +#' The provided functions evaluate such expressions in an environment (dictionary) which holds the following information: |
| 9 | +#' \itemize{ |
| 10 | +#' \item{\code{task}:} the task itself, allowing to access any of its elements. |
| 11 | +#' \item{\code{p}:} the number of features in the task |
| 12 | +#' \item{\code{n}:} the number of observations in the task |
| 13 | +#' \item{\code{type}:} the task type, i.e. "classif", "regr", "surv", "cluster", "costcens" or "multilabel" |
| 14 | +#' \item{\code{k}:} the number of classes of the target variable (only available for classification tasks) |
| 15 | +#' } |
| 16 | +#' Usually the evaluation of the expression is performed automatically, e.g. in \code{\link{train}} or |
| 17 | +#' \code{\link{tuneParams}}. |
| 18 | +#' Therefore calling \code{evaluateParamSet} or \code{evaluateLearner} manually should not be necessary. |
| 19 | +#' |
5 | 20 | #' @template arg_learner |
6 | 21 | #' @param par.set [\code{\link[ParamHelpers]{ParamSet}}]\cr |
7 | 22 | #' Parameter set of (hyper)parameters and their constraints. |
|
11 | 26 | #' @return [\code{\link{Learner}} | \code{\link[ParamHelpers]{ParamSet}}]. |
12 | 27 | #' @name evaluateLearner |
13 | 28 | #' @rdname evaluateLearner |
14 | | -#' @details The expressions can be based on any information provided by the task. For convenience, |
15 | | -#' the most often used keys are available directly |
16 | | -#' \itemize{ |
17 | | -#' \item{\code{task}:} the task itself, allowing to access any of its elements |
18 | | -#' \item{\code{p}:} the number of features in the task |
19 | | -#' \item{\code{n}:} the number of observations in the task |
20 | | -#' \item{\code{type}:} the task type, i.e. "classif", "regr", "surv", "cluster", "costcens" or "multilabel" |
21 | | -#' \item{\code{k}:} the number of classes of the target variable (only available for classification tasks) |
22 | | -#' } |
23 | | -#' However, if one wants to access any other parts of the \code{task}, one can do so. For instance, one could |
24 | | -#' access the "blocking" via \code{task$task.desc$has.blocking}. |
| 29 | +#' @export |
25 | 30 | #' @examples |
26 | 31 | #' ## (1) evaluation of a learner's hyperparameters |
27 | 32 | #' task = makeClassifTask(data = iris, target = "Species") |
28 | 33 | #' lrn1 = makeLearner("classif.rpart", minsplit = expression(k * p), |
29 | 34 | #' minbucket = expression(3L + 4L * task$task.desc$has.blocking)) |
30 | 35 | #' lrn2 = evaluateLearner(learner = lrn1, task = task) |
31 | | -#' |
32 | | -#' lrn1$par.vals |
33 | | -#' lrn2$par.vals |
34 | | -#' |
| 36 | +#' |
| 37 | +#' getHyperPars(lrn1) |
| 38 | +#' getHyperPars(lrn2) |
| 39 | +#' |
35 | 40 | #' ## (2) evaluation of a learner's entire parameter set |
36 | 41 | #' task = makeClassifTask(data = iris, target = "Species") |
37 | 42 | #' lrn1 = makeLearner("classif.randomForest") |
38 | 43 | #' lrn2 = evaluateLearner(learner = lrn1, task = task) |
39 | | -#' |
40 | | -#' ## focus on the parameters 'mtry', 'classwt' and 'cutoff' |
41 | | -#' lrn1$par.set |
42 | | -#' lrn2$par.set |
43 | | -#' |
| 44 | +#' |
| 45 | +#' ## Note the values for parameters 'mtry', 'classwt' and 'cutoff' |
| 46 | +#' getParamSet(lrn1) |
| 47 | +#' getParamSet(lrn2) |
| 48 | +#' |
44 | 49 | #' ## (3) evaluation of a parameter set |
45 | 50 | #' task = makeClassifTask(data = iris, target = "Species") |
46 | 51 | #' ps1 = makeParamSet( |
47 | 52 | #' makeNumericParam("C", lower = expression(k), upper = expression(n), trafo = function(x) 2^x), |
48 | 53 | #' makeDiscreteParam("sigma", values = expression(list(k, p))) |
49 | 54 | #' ) |
50 | | -#' ps2 = evaluateParset(par.set = ps1, task = task) |
51 | | -#' @export |
| 55 | +#' evaluateParset(par.set = ps1, task = task) |
52 | 56 | evaluateLearner = function(learner, task) { |
53 | 57 | dict = makeTaskDictionary(task = task) |
54 | 58 | learner$par.set = evaluateParset(learner$par.set, task = task) |
55 | | - if (length(learner$par.vals) > 0 && any(vlapply(learner$par.vals, is.expression))) |
| 59 | + if (any(vlapply(learner$par.vals, is.expression))) |
56 | 60 | learner$par.vals = lapply(learner$par.vals, function(expr) eval(expr, envir = dict)) |
57 | 61 | return(learner) |
58 | 62 | } |
59 | 63 |
|
60 | 64 | #' @rdname evaluateLearner |
61 | 65 | #' @export |
62 | 66 | evaluateParset = function(par.set, task) { |
63 | | - dict = makeTaskDictionary(task = task) |
64 | 67 | if (hasExpression(par = par.set)) { |
| 68 | + dict = makeTaskDictionary(task = task) |
65 | 69 | checkParamSet(par.set = par.set, dict = dict) |
66 | 70 | par.set = evaluateParamSet(par.set = par.set, dict = dict) |
67 | 71 | ## assure that the value names are also shown if the values list was unnamed |
68 | 72 | par.set$pars = lapply(par.set$pars, function(x) { |
69 | | - if (is.null(x$values) || !is.null(names(x$values))) |
70 | | - return(x) |
71 | | - names(x$values) = unlist(lapply(x$values, function(vals) vals)) |
| 73 | + if (!is.null(x$values) && is.null(names(x$values))) |
| 74 | + names(x$values) = unlist(x$values) |
72 | 75 | return(x) |
73 | 76 | }) |
74 | 77 | } |
|
0 commit comments