Classification based on the Bernoulli Naive Bayes model.
object of class inheriting from
matrix with numeric 0-1 predictors.
if "class", new data points are classified according to the highest posterior probabilities. If "prob", the posterior probabilities for each class are returned.
This is a specialized version of the Naive Bayes classifier, in which all features take on numeric 0-1 values and class conditional probabilities are modelled with the Bernoulli distribution.
Class posterior probabilities are calculated using the Bayes' rule under the assumption of independence of predictors. If no
newdata is provided, the data from the object is used.
The Bernoulli Naive Bayes is available in both,
bernoulli_naive_bayes. The implementation of the specialized Naive Bayes provides more efficient performance though. The speedup comes from the restricting the data input to a numeric 0-1 matrix and performing the linear algebra as well as vectorized operations on it. In other words, the efficiency comes at cost of the flexibility.
The NAs in the newdata are not included into the calculation of posterior probabilities; and if present an informative warning is given.
bernoulli_naive_bayes function is equivalent to the
naive_bayes function with the numeric 0-1 matrix being coerced, for instance, to the "0"-"1" character matrix.
predict.bernoulli_naive_bayes returns either a factor with class labels corresponding to the maximal conditional posterior probabilities or a matrix with class label specific conditional posterior probabilities.
Michal Majka, firstname.lastname@example.org
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
cols <- 10 ; rows <- 100 ; probs <- c("0" = 0.4, "1" = 0.1) M <- matrix(sample(0:1, rows * cols, TRUE, probs), nrow = rows, ncol = cols) y <- factor(sample(paste0("class", LETTERS[1:2]), rows, TRUE, prob = c(0.3,0.7))) colnames(M) <- paste0("V", seq_len(ncol(M))) laplace <- 0.5 ### Train the Bernoulli Naive Bayes bnb <- bernoulli_naive_bayes(x = M, y = y, laplace = laplace) ### Classification head(predict(bnb, newdata = M, type = "class")) head(bnb %class% M) ### Posterior probabilities head(predict(bnb, newdata = M, type = "prob")) head(bnb %prob% M)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.