Expand description

Multinomial Naive Bayes

Multinomial Naive Bayes classifier is a variant of Naive Bayes for the multinomially distributed data. It is often used for discrete data with predictors representing the number of times an event was observed in a particular instance, for example frequency of the words present in the document.

Example:

use smartcore::linalg::basic::matrix::DenseMatrix;
use smartcore::naive_bayes::multinomial::MultinomialNB;

// Training data points are:
// Chinese Beijing Chinese (class: China)
// Chinese Chinese Shanghai (class: China)
// Chinese Macao (class: China)
// Tokyo Japan Chinese (class: Japan)
let x = DenseMatrix::<u32>::from_2d_array(&[
                  &[1, 2, 0, 0, 0, 0],
                  &[0, 2, 0, 0, 1, 0],
                  &[0, 1, 0, 1, 0, 0],
                  &[0, 1, 1, 0, 0, 1],
        ]);
let y: Vec<u32> = vec![0, 0, 0, 1];
let nb = MultinomialNB::fit(&x, &y, Default::default()).unwrap();

// Testing data point is:
//  Chinese Chinese Chinese Tokyo Japan
let x_test = DenseMatrix::from_2d_array(&[&[0, 3, 1, 0, 0, 1]]);
let y_hat = nb.predict(&x_test).unwrap();

References:

Structs