6 releases (3 breaking)

0.6.0 Jun 15, 2022
0.5.1 Mar 1, 2022
0.5.0 Oct 21, 2021
0.4.0 Apr 28, 2021
0.3.0 Jan 20, 2021

#27 in Machine learning

Download history 25/week @ 2022-03-16 14/week @ 2022-03-23 8/week @ 2022-03-30 7/week @ 2022-04-06 6/week @ 2022-04-13 6/week @ 2022-04-20 40/week @ 2022-04-27 28/week @ 2022-05-04 84/week @ 2022-05-11 28/week @ 2022-05-18 31/week @ 2022-05-25 44/week @ 2022-06-01 17/week @ 2022-06-08 37/week @ 2022-06-15 20/week @ 2022-06-22 4/week @ 2022-06-29

79 downloads per month
Used in linfa-preprocessing

MIT/Apache

230KB
4.5K SLoC

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:

Examples

You can find examples in the examples/ directory. To run Gaussian Naive Bayes example, use:

$ cargo run --example winequality --release
Show source code
use linfa::metrics::ToConfusionMatrix;
use linfa::traits::{Fit, Predict};
use linfa_bayes::{GaussianNb, Result};

// Read in the dataset and convert targets to binary data
let (train, valid) = linfa_datasets::winequality()
    .map_targets(|x| if *x > 6 { "good" } else { "bad" })
    .split_with_ratio(0.9);

// Train the model
let model = GaussianNb::params().fit(&train)?;

// Predict the validation dataset
let pred = model.predict(&valid);

// Construct confusion matrix
let cm = pred.confusion_matrix(&valid)?;

// classes    | bad        | good      
// bad        | 130        | 12        
// good       | 7          | 10    
//
// accuracy 0.8805031, MCC 0.45080978
println!("{:?}", cm);
println!("accuracy {}, MCC {}", cm.accuracy(), cm.mcc());
# Result::Ok(())

To run Multinomial Naive Bayes example, use:

$ cargo run --example winequality_multinomial --release
Show source code
use linfa::metrics::ToConfusionMatrix;
use linfa::traits::{Fit, Predict};
use linfa_bayes::{MultinomialNb, Result};

// Read in the dataset and convert targets to binary data
let (train, valid) = linfa_datasets::winequality()
    .map_targets(|x| if *x > 6 { "good" } else { "bad" })
    .split_with_ratio(0.9);

// Train the model
let model = MultinomialNb::params().fit(&train)?;

// Predict the validation dataset
let pred = model.predict(&valid);

// Construct confusion matrix
let cm = pred.confusion_matrix(&valid)?;
// classes    | bad        | good      
// bad        | 88         | 54        
// good       | 10         | 7         

// accuracy 0.5974843, MCC 0.02000631
println!("{:?}", cm);
println!("accuracy {}, MCC {}", cm.accuracy(), cm.mcc());
# Result::Ok(())

Dependencies

~3.5MB
~70K SLoC