8 releases (4 breaking)

0.7.0 Oct 16, 2023
0.6.1 Dec 3, 2022
0.6.0 Jun 15, 2022
0.5.1 Mar 1, 2022
0.3.0 Jan 20, 2021

#512 in Machine learning

Download history 73/week @ 2024-07-29 49/week @ 2024-08-26 5/week @ 2024-09-02 22/week @ 2024-09-09 15/week @ 2024-09-16 52/week @ 2024-09-23 9/week @ 2024-09-30 10/week @ 2024-10-07 24/week @ 2024-10-14 22/week @ 2024-10-21 20/week @ 2024-10-28 38/week @ 2024-11-04

105 downloads per month
Used in 2 crates

MIT/Apache

240KB
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

~5MB
~96K SLoC