Learn R Programming

targeted (version 0.6)

learner_naivebayes: Construct a learner

Description

Constructs a learner class object for fitting a naive bayes classifier with naivebayes. As shown in the examples, the constructed learner returns predicted class probabilities of class 2 in case of binary classification. A n times p matrix, with n being the number of observations and p the number of classes, is returned for multi-class classification.

Usage

learner_naivebayes(
  formula,
  info = "Naive Bayes",
  laplace.smooth = 0,
  kernel = FALSE,
  learner.args = NULL,
  ...
)

Value

learner object.

Arguments

formula

(formula) Formula specifying response and design matrix.

info

(character) Optional information to describe the instantiated learner object.

laplace.smooth

Laplace smoothing

kernel

If TRUE a kernel estimator is used for numeric predictors (otherwise a gaussian model is used)

learner.args

(list) Additional arguments to learner$new().

...

Additional arguments to naivebayes.

Examples

Run this code
n <- 5e2
x1 <- rnorm(n, sd = 2)
x2 <- rnorm(n)
y <- rbinom(n, 1, lava::expit(x2*x1 + cos(x1)))
d <- data.frame(y, x1, x2)

# binary classification
lr <- learner_naivebayes(y ~ x1 + x2)
lr$estimate(d)
lr$predict(head(d))

# multi-class classification
lr <- learner_naivebayes(Species ~ .)
lr$estimate(iris)
lr$predict(head(iris))

Run the code above in your browser using DataLab