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)
Run the code above in your browser using DataLab