File: DummyFeaturesWrapper.R

package info (click to toggle)
r-cran-mlr 2.19.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,264 kB
  • sloc: ansic: 65; sh: 13; makefile: 5
file content (37 lines) | stat: -rw-r--r-- 1,256 bytes parent folder | download
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
#' @title Fuse learner with dummy feature creator.
#'
#' @description
#' Fuses a base learner with the dummy feature creator (see [createDummyFeatures]).
#' Returns a learner which can be used like any other learner.
#'
#' @template arg_learner
#' @inheritParams createDummyFeatures
#' @template ret_learner
#' @family wrapper
#' @export
makeDummyFeaturesWrapper = function(learner, method = "1-of-n", cols = NULL) {

  learner = checkLearner(learner)
  args = list(method = method, cols = cols)
  rm(list = names(args))

  trainfun = function(data, target, args) {
    data = do.call(createDummyFeatures, c(list(obj = data, target = target), args))
    return(list(data = data, control = list()))
  }

  predictfun = function(data, target, args, control) {
    y = intersect(target, colnames(data))
    data = do.call(createDummyFeatures, c(list(obj = data, target = y), args))
    return(data)
  }

  lrn = makePreprocWrapper(learner, trainfun, predictfun, par.vals = args)
  lrn$id = stri_replace(lrn$id, replacement = ".dummied", regex = "\\.preproc$")
  addClasses(lrn, "DummyFeaturesWrapper")
}

#' @export
getLearnerProperties.DummyFeaturesWrapper = function(learner) {
  union(getLearnerProperties(learner$next.learner), c("factors", "ordered"))
}