| Crates.io | linfa-bayes |
| lib.rs | linfa-bayes |
| version | 0.7.1 |
| created_at | 2021-01-20 18:59:12.997964+00 |
| updated_at | 2025-01-14 15:41:49.505536+00 |
| description | Collection of Naive Bayes Algorithms |
| homepage | |
| repository | https://github.com/rust-ml/linfa |
| max_upload_size | |
| id | 344540 |
| size | 54,374 |
linfa-bayes provides pure Rust implementations of Naive Bayes algorithms for the Linfa toolkit.
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.
linfa-bayes currently provides an implementation of the following methods:
GaussianNb])MultinomialNb]))You can find examples in the examples/ directory. To run Gaussian Naive Bayes example, use:
$ cargo run --example winequality --release
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
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(())