File: RLearner_classif_rotationForest.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 (36 lines) | stat: -rw-r--r-- 1,337 bytes parent folder | download | duplicates (3)
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
#' @export
makeRLearner.classif.rotationForest = function() {
  makeRLearnerClassif(
    cl = "classif.rotationForest",
    package = "rotationForest",
    par.set = makeParamSet(
      makeIntegerLearnerParam(id = "K", default = 3L, lower = 1L),
      makeIntegerLearnerParam(id = "L", default = 10L, lower = 1L)
    ),
    properties = c("twoclass", "numerics", "factors", "ordered", "prob"),
    name = "Rotation Forest",
    short.name = "rotationForest",
    callees = "rotationForest"
  )
}

#' @export
trainLearner.classif.rotationForest = function(.learner, .task, .subset, .weights = NULL, ...) {
  df = getTaskData(.task, .subset, target.extra = TRUE)
  features = df$data
  # rotationForest needs 0-1 coding
  target = as.factor(ifelse(df$target == .task$task.desc$positive, 1L, 0L))
  rotationForest::rotationForest(x = features, y = target, ...)
}

#' @export
predictLearner.classif.rotationForest = function(.learner, .model, .newdata, ...) {
  features = .newdata[, names(.newdata) == .model$features]
  p = predict(.model$learner.model, newdata = features, all = FALSE, ...)
  if (.learner$predict.type == "prob") {
    levs = c(.model$task.desc$positive, .model$task.desc$negative)
    propVectorToMatrix(1 - p, levs)
  } else {
    as.factor(ifelse(p > 0.5, .model$task.desc$positive, .model$task.desc$negative))
  }
}