Mirrors the C# NaiveBayes class: assumes each feature is normally distributed given the
class and independent of the other features, then assigns each new observation the class with
the highest posterior probability.
Usage
ml_naive_bayes(x, y, newdata = NULL)
Arguments
- x
a numeric matrix or data frame of training predictors, one row per observation.
- y
the training class labels, one per row of x.
- newdata
predictors to classify, with the same number of columns as x. NULL (the
default) trains without predicting.
Value
a list with classes, means and standard_deviations (both one row per class and
one column per feature), priors, and – when newdata is supplied – prediction, the
predicted class label for each row of newdata.
Details
classes follows the order the class labels FIRST APPEAR in y, not sorted order, and every
per-class row of means, standard_deviations and priors is indexed to match. A class with
a single member gets a standard deviation of 1e-6 rather than 0.
Examples
x <- c(1, 1.1, 0.9, 1.2, 1.05, 5, 5.1, 4.9, 5.2, 5.05)
y <- c(0, 0, 0, 0, 0, 1, 1, 1, 1, 1)
ml_naive_bayes(x, y, newdata = c(1.0, 5.0))$prediction
#> [1] 0 1