Naive Bayes
linfa-bayes provides pure Rust implementations of Naive Bayes algorithms for the Linfa toolkit.
The Big Picture
linfa-bayes is a crate in the linfa ecosystem, an effort to create a toolkit for classical Machine Learning implemented in pure Rust, akin to Python's scikit-learn.
Current state
linfa-bayes currently provides an implementation of the following methods:
- Gaussian Naive Bayes ([
GaussianNb]) - Multinomial Naive Nayes ([
MultinomialNb]) - Bernoulli Naive Nayes ([
BernoulliNb])
Examples
You can find examples in the examples/ directory. To run Gaussian Naive Bayes example, use:
use ToConfusionMatrix;
use ;
use ;
// Read in the dataset and convert targets to binary data
let = winequality
.map_targets
.split_with_ratio;
// Train the model
let model = params.fit?;
// Predict the validation dataset
let pred = model.predict;
// Construct confusion matrix
let cm = pred.confusion_matrix?;
// classes | bad | good
// bad | 130 | 12
// good | 7 | 10
//
// accuracy 0.8805031, MCC 0.45080978
println!;
println!;
# ResultOk
To run Multinomial Naive Bayes example, use:
use ToConfusionMatrix;
use ;
use ;
// Read in the dataset and convert targets to binary data
let = winequality
.map_targets
.split_with_ratio;
// Train the model
let model = params.fit?;
// Predict the validation dataset
let pred = model.predict;
// Construct confusion matrix
let cm = pred.confusion_matrix?;
// classes | bad | good
// bad | 88 | 54
// good | 10 | 7
// accuracy 0.5974843, MCC 0.02000631
println!;
println!;
# ResultOk
To run Bernoulli Naive Bayes example, use:
use ToConfusionMatrix;
use ;
use ;
// Read in the dataset and convert targets to binary data
let = winequality
.map_targets
.split_with_ratio;
// Train the model
let model = params.fit?;
// Predict the validation dataset
let pred = model.predict;
// Construct confusion matrix
let cm = pred.confusion_matrix?;
// classes | bad | good
// bad | 142 | 0
// good | 17 | 0
// accuracy 0.8930818, MCC NaN
println!;
println!;
# ResultOk