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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
#' @title Return task ids used in benchmark.
#'
#' @description
#' Gets the task IDs used in a benchmark experiment.
#'
#' @template arg_bmr
#' @return ([character]).
#' @export
#' @family benchmark
getBMRTaskIds = function(bmr) {
assertClass(bmr, "BenchmarkResult")
return(names(bmr$results))
}
#' @title Return learners used in benchmark.
#'
#' @description
#' Gets the learners used in a benchmark experiment.
#'
#' @template arg_bmr
#' @return ([list]).
#' @export
#' @family benchmark
getBMRLearners = function(bmr) {
assertClass(bmr, "BenchmarkResult")
return(bmr$learners)
}
#' @title Return learner ids used in benchmark.
#'
#' @description
#' Gets the IDs of the learners used in a benchmark experiment.
#'
#' @template arg_bmr
#' @return ([character]).
#' @export
#' @family benchmark
getBMRLearnerIds = function(bmr) {
assertClass(bmr, "BenchmarkResult")
extractSubList(bmr$learners, "id", use.names = FALSE)
}
#' @title Return learner short.names used in benchmark.
#'
#' @description
#' Gets the learner short.names of the learners used in a benchmark experiment.
#'
#' @template arg_bmr
#' @return ([character]).
#' @export
#' @family benchmark
getBMRLearnerShortNames = function(bmr) {
assertClass(bmr, "BenchmarkResult")
vcapply(bmr$learners, getLearnerShortName, use.names = FALSE)
}
#' @title Return measures used in benchmark.
#'
#' @description
#' Gets the measures used in a benchmark experiment.
#'
#' @template arg_bmr
#' @return ([list]). See above.
#' @export
#' @family benchmark
getBMRMeasures = function(bmr) {
assertClass(bmr, "BenchmarkResult")
return(bmr$measures)
}
#' @title Return measures IDs used in benchmark.
#'
#' @description
#' Gets the IDs of the measures used in a benchmark experiment.
#'
#' @template arg_bmr
#' @return ([list]). See above.
#' @export
#' @family benchmark
getBMRMeasureIds = function(bmr) {
assertClass(bmr, "BenchmarkResult")
extractSubList(bmr$measures, "id", use.names = FALSE)
}
# returns buried object in BMR, either as list of lists or data.frame with task.id, learner.id cols
# you can restrict to subsets for tasks and learners and pass function to extract object
getBMRObjects = function(bmr, task.ids = NULL, learner.ids = NULL, fun, as.df = FALSE, drop = FALSE) {
assertClass(bmr, "BenchmarkResult")
brtids = getBMRTaskIds(bmr)
brlids = getBMRLearnerIds(bmr)
if (is.null(task.ids)) {
task.ids = brtids
} else {
assertSubset(task.ids, brtids)
}
if (is.null(learner.ids)) {
learner.ids = brlids
} else {
assertSubset(learner.ids, brlids)
}
res = lapply(task.ids, function(tid) {
xs = lapply(learner.ids, function(lid) {
p = fun(bmr$results[[tid]][[lid]])
if (as.df) {
if (!is.null(p)) {
p = as.data.frame(cbind(task.id = tid, learner.id = lid, p))
}
}
return(p)
})
if (as.df) {
xs = setDF(rbindlist(xs, fill = TRUE, use.names = TRUE))
} else {
xs = setNames(xs, learner.ids)
}
return(xs)
})
if (as.df) {
res = setDF(rbindlist(res, fill = TRUE, use.names = TRUE))
} else {
res = setNames(res, task.ids)
if (drop) {
# when drop is on we check if learner.ids and/or task.ids are of length 1
# if so the list is unnested
drop.tasks = length(task.ids) == 1L
drop.learners = length(learner.ids) == 1L
if (drop.tasks | drop.learners) {
res = unlist(res, recursive = FALSE)
if (drop.tasks & drop.learners) {
res = res[[1L]]
}
if (drop.tasks & !drop.learners) {
res = setNames(res, learner.ids)
}
if (!drop.tasks & drop.learners) {
res = setNames(res, task.ids)
}
}
}
}
return(res)
}
#' @title Extract the predictions from a benchmark result.
#'
#' @description
#' Either a list of lists of [ResamplePrediction] objects, as returned by
#' [resample], or these objects are rbind-ed with extra columns
#' \dQuote{task.id} and \dQuote{learner.id}.
#'
#' If `predict.type` is \dQuote{prob}, the probabilities for each class are returned in addition to the response.
#'
#' If `keep.pred` is `FALSE` in the call to [benchmark], the function will return `NULL`.
#'
#' @template arg_bmr
#' @template arg_bmr_taskids
#' @template arg_bmr_learnerids
#' @template arg_bmr_asdf
#' @template arg_bmr_drop
#' @template ret_bmr_list_or_df
#' @export
#' @family benchmark
getBMRPredictions = function(bmr, task.ids = NULL, learner.ids = NULL, as.df = FALSE, drop = FALSE) {
assertClass(bmr, "BenchmarkResult")
f = if (as.df) {
function(x) as.data.frame(getRRPredictions(x))
} else {
function(x) getRRPredictions(x)
}
getBMRObjects(bmr, task.ids, learner.ids, fun = f, as.df = as.df, drop = drop)
}
#' @title Extract the test performance values from a benchmark result.
#'
#' @description
#' Either a list of lists of \dQuote{measure.test} data.frames, as returned by
#' [resample], or these objects are rbind-ed with extra columns
#' \dQuote{task.id} and \dQuote{learner.id}.
#'
#'
#' @template arg_bmr
#' @template arg_bmr_taskids
#' @template arg_bmr_learnerids
#' @template arg_bmr_asdf
#' @template arg_bmr_drop
#' @template ret_bmr_list_or_df
#' @export
#' @family benchmark
getBMRPerformances = function(bmr, task.ids = NULL, learner.ids = NULL, as.df = FALSE, drop = FALSE) {
assertClass(bmr, "BenchmarkResult")
f = function(x) x$measures.test
getBMRObjects(bmr, task.ids, learner.ids, fun = f, as.df = as.df, drop = drop)
}
#' @title Extract the aggregated performance values from a benchmark result.
#'
#' @description
#' Either a list of lists of \dQuote{aggr} numeric vectors, as returned by
#' [resample], or these objects are rbind-ed with extra columns
#' \dQuote{task.id} and \dQuote{learner.id}.
#'
#'
#' @template arg_bmr
#' @template arg_bmr_taskids
#' @template arg_bmr_learnerids
#' @template arg_bmr_asdf
#' @template arg_bmr_drop
#' @template ret_bmr_list_or_df
#' @export
#' @family benchmark
getBMRAggrPerformances = function(bmr, task.ids = NULL, learner.ids = NULL, as.df = FALSE, drop = FALSE) {
assertClass(bmr, "BenchmarkResult")
f = if (as.df) {
function(x) as.data.frame(as.list(x$aggr))
} else {
function(x) x$aggr
}
getBMRObjects(bmr, task.ids, learner.ids, fun = f, as.df = as.df, drop = drop)
}
getBMROptResults = function(bmr, task.ids = NULL, learner.ids = NULL, as.df = FALSE,
wrapper.class, fun, drop = FALSE) {
f = if (as.df) {
function(x) {
if (inherits(x$learner, wrapper.class)) {
xs = lapply(x$extract, fun)
xs = setDF(rbindlist(lapply(seq_along(xs), function(i) cbind(iter = i, xs[[i]])), fill = TRUE, use.names = TRUE))
} else {
NULL
}
}
} else {
function(x) {
if (inherits(x$learner, wrapper.class)) {
x$extract
} else {
NULL
}
}
}
getBMRObjects(bmr, task.ids, learner.ids, fun = f, as.df = as.df, drop = drop)
}
#' @title Extract the tuning results from a benchmark result.
#'
#' @description
#' Returns a nested list of [TuneResult]s. The first level of nesting is by data set, the second by learner, the third for the benchmark resampling iterations. If `as.df` is `TRUE`, a data frame with the \dQuote{task.id}, \dQuote{learner.id}, the resample iteration, the parameter values and the performances is returned.
#'
#' @template arg_bmr
#' @template arg_bmr_taskids
#' @template arg_bmr_learnerids
#' @template arg_bmr_asdf
#' @template arg_bmr_drop
#' @template ret_bmr_list_or_df
#' @export
#' @family benchmark
getBMRTuneResults = function(bmr, task.ids = NULL, learner.ids = NULL, as.df = FALSE, drop = FALSE) {
assertClass(bmr, "BenchmarkResult")
getBMROptResults(bmr, task.ids, learner.ids, as.df, "TuneWrapper", function(x) {
data.frame(x$x, as.list(x$y))
}, drop = drop)
}
#' @title Extract the feature selection results from a benchmark result.
#'
#' @description
#' Returns a nested list of [FeatSelResult]s. The first level of nesting is by data set, the second by learner, the third for the benchmark resampling iterations. If `as.df` is `TRUE`, a data frame with \dQuote{task.id}, \dQuote{learner.id}, the resample iteration and the selected features is returned.
#'
#' Note that if more than one feature is selected and a data frame is requested, there will be multiple rows for the same dataset-learner-iteration; one for each selected feature.
#'
#' @template arg_bmr
#' @template arg_bmr_taskids
#' @template arg_bmr_learnerids
#' @template arg_bmr_asdf
#' @template arg_bmr_drop
#' @template ret_bmr_list_or_df
#' @export
#' @family benchmark
getBMRFeatSelResults = function(bmr, task.ids = NULL, learner.ids = NULL, as.df = FALSE, drop = FALSE) {
assertClass(bmr, "BenchmarkResult")
getBMROptResults(bmr, task.ids, learner.ids, as.df, "FeatSelWrapper", function(x) {
as.data.frame(x$x)
}, drop = drop)
}
#' @title Extract the feature selection results from a benchmark result.
#'
#' @description
#' Returns a nested list of characters The first level of nesting is by data set, the second by learner, the third for the benchmark resampling iterations. The list at the lowest level is the list of selected features. If `as.df` is `TRUE`, a data frame with \dQuote{task.id}, \dQuote{learner.id}, the resample iteration and the selected features is returned.
#'
#' Note that if more than one feature is selected and a data frame is requested, there will be multiple rows for the same dataset-learner-iteration; one for each selected feature.
#'
#' @template arg_bmr
#' @template arg_bmr_taskids
#' @template arg_bmr_learnerids
#' @template arg_bmr_asdf
#' @template arg_bmr_drop
#' @template ret_bmr_list_or_df
#' @export
#' @family benchmark
getBMRFilteredFeatures = function(bmr, task.ids = NULL, learner.ids = NULL, as.df = FALSE, drop = FALSE) {
assertClass(bmr, "BenchmarkResult")
getBMROptResults(bmr, task.ids, learner.ids, as.df, "FilterWrapper", function(x) {
as.data.frame(x)
}, drop = drop)
}
#' @title Extract all models from benchmark result.
#'
#' @description
#' A list of lists containing all [WrappedModel]s trained in the benchmark experiment.
#'
#' If `models` is `FALSE` in the call to [benchmark], the function will return `NULL`.
#'
#' @template arg_bmr
#' @template arg_bmr_taskids
#' @template arg_bmr_learnerids
#' @template arg_bmr_drop
#' @return ([list]).
#' @export
#' @family benchmark
getBMRModels = function(bmr, task.ids = NULL, learner.ids = NULL, drop = FALSE) {
assertClass(bmr, "BenchmarkResult")
f = function(x) {
x$models
}
getBMRObjects(bmr, task.ids, learner.ids, fun = f, as.df = FALSE, drop = drop)
}
#' @title Extract all task descriptions from benchmark result (DEPRECATED).
#'
#' @description
#' A list containing all [TaskDesc]s for each task contained in the benchmark experiment.
#' @template arg_bmr
#' @return ([list]).
#' @export
getBMRTaskDescriptions = function(bmr) {
.Deprecated("getBMRTaskDesc")
getBMRTaskDescs(bmr)
}
#' @title Extract all task descriptions from benchmark result.
#'
#' @description
#' A list containing all [TaskDesc]s for each task contained in the benchmark experiment.
#' @template arg_bmr
#' @return ([list]).
#' @export
#' @family benchmark
getBMRTaskDescs = function(bmr) {
lapply(bmr$results, function(x) lapply(x, getRRTaskDesc))
}
|