File: RLearner_classif_naiveBayes.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 (27 lines) | stat: -rw-r--r-- 925 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
#' @export
makeRLearner.classif.naiveBayes = function() {
  makeRLearnerClassif(
    cl = "classif.naiveBayes",
    package = "e1071",
    par.set = makeParamSet(
      makeNumericLearnerParam(id = "laplace", default = 0, lower = 0)
      # makeNumericLearnerParam(id = "threshold", default = 0.001, lower = 0)
    ),
    properties = c("twoclass", "multiclass", "missings", "numerics", "factors", "prob"),
    name = "Naive Bayes",
    short.name = "nbayes",
    callees = "naiveBayes"
  )
}

#' @export
trainLearner.classif.naiveBayes = function(.learner, .task, .subset, .weights = NULL, ...) {
  f = getTaskFormula(.task)
  e1071::naiveBayes(f, data = getTaskData(.task, .subset), ...)
}

#' @export
predictLearner.classif.naiveBayes = function(.learner, .model, .newdata, ...) {
  type = ifelse(.learner$predict.type == "response", "class", "raw")
  predict(.model$learner.model, newdata = .newdata, type = type, ...)
}