/
WrappedModel.R
153 lines (143 loc) · 4.65 KB
/
WrappedModel.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#' @title Induced model of learner.
#'
#' @description
#' Result from [train].
#'
#' It internally stores the underlying fitted model,
#' the subset used for training, features used for training, levels of factors in the
#' data set and computation time that was spent for training.
#'
#' Object members: See arguments.
#'
#' The constructor `makeWrappedModel` is mainly for internal use.
#'
#' @template arg_learner
#' @param learner.model (any)\cr
#' Underlying model.
#' @template arg_taskdesc
#' @template arg_subset
#' @param features ([character])\cr
#' Features used for training.
#' @param factor.levels (named [list] of [character])\cr
#' Levels of factor variables (features and potentially target) in training data.
#' Named by variable name, non-factors do not occur in the list.
#' @param time (`numeric(1)`)\cr
#' Computation time for model fit in seconds.
#' @template ret_wmodel
#' @export
#' @aliases WrappedModel
makeWrappedModel = function(learner, learner.model, task.desc, subset, features, factor.levels, time) {
UseMethod("makeWrappedModel")
}
#' @export
makeWrappedModel.Learner = function(learner, learner.model, task.desc, subset, features, factor.levels, time) {
dump = NULL
if (is.error(learner.model)) {
learner.model = as.character(learner.model)
time = NA_real_
cl = c("FailureModel", "WrappedModel")
if (getLearnerOptions(learner, "on.error.dump")$on.error.dump) {
dump = addClasses(get("last.dump", envir = .GlobalEnv), "mlr.dump")
}
} else {
cl = "WrappedModel"
}
makeS3Obj(cl,
learner = learner,
learner.model = learner.model,
task.desc = task.desc,
subset = subset,
features = features,
factor.levels = factor.levels,
time = time,
dump = dump
)
}
#' @export
print.WrappedModel = function(x, ...) {
cat(
"Model for learner.id=", x$learner$id, "; learner.class=", getClass1(x$learner), "\n",
sprintf("Trained on: task.id = %s; obs = %i; features = %i",
x$task.desc$id, length(x$subset), length(x$features)), "\n",
"Hyperparameters: ", getHyperParsString(x$learner, show.missing.values = TRUE), "\n",
sep = ""
)
if (isFailureModel(x)) {
catf("Training failed: %s", getFailureModelMsg(x))
}
}
#' Get underlying R model of learner integrated into mlr.
#'
#' @param model ([WrappedModel])\cr
#' The model, returned by e.g., [train].
#' @param more.unwrap (`logical(1)`)\cr
#' Some learners are not basic learners from R, but implemented in mlr as meta-techniques.
#' Examples are everything that inherits from `HomogeneousEnsemble`.
#' In these cases, the `learner.model` is often a list of mlr [WrappedModel]s.
#' This option allows to strip them further to basic R models.
#' The option is simply ignored for basic learner models.
#' Default is `FALSE`.
#' @return (any). A fitted model, depending the learner / wrapped package. E.g., a
#' model of class [rpart::rpart] for learner \dQuote{classif.rpart}.
#' @export
getLearnerModel = function(model, more.unwrap = FALSE) {
assertFlag(more.unwrap)
UseMethod("getLearnerModel")
}
#' @export
getLearnerModel.WrappedModel = function(model, more.unwrap) {
model$learner.model
}
#' @title Is the model a FailureModel?
#'
#' @description
#' Such a model is created when one sets the corresponding option in [configureMlr].
#'
#' For complex wrappers this getter returns `TRUE` if ANY model contained in it failed.
#'
#' @template arg_wrappedmod
#' @return (`logical(1)`).
#' @export
isFailureModel = function(model) {
UseMethod("isFailureModel")
}
#' @export
# by default the model is never a failure. if a failure happens we have the derived class FailureModel
isFailureModel.WrappedModel = function(model) {
return(FALSE)
}
#' @title Return error message of FailureModel.
#'
#' @description
#' Such a model is created when one sets the corresponding option in [configureMlr].
#' If no failure occurred, `NA` is returned.
#'
#' For complex wrappers this getter returns the first error message encountered in ANY model that failed.
#'
#' @template arg_wrappedmod
#' @return (`character(1)`).
#' @export
getFailureModelMsg = function(model) {
UseMethod("getFailureModelMsg")
}
#' @export
getFailureModelMsg.WrappedModel = function(model) {
return(NA_character_)
}
#' @title Return the error dump of FailureModel.
#'
#' @description
#' Returns the error dump that can be used with `debugger()` to evaluate errors.
#' If [configureMlr] configuration `on.error.dump` is `FALSE`, this returns
#' `NULL`.
#'
#' @template arg_wrappedmod
#' @return (`last.dump`).
#' @export
getFailureModelDump = function(model) {
UseMethod("getFailureModelDump")
}
#' @export
getFailureModelDump.WrappedModel = function(model) {
return(NULL)
}