8 个版本 (4 个重大更新)

0.7.0 2023年10月16日
0.6.1 2022年12月3日
0.6.0 2022年6月15日
0.5.1 2022年3月1日
0.3.0 2021年1月20日

#250 in 机器学习

每月 35 次下载
linfa-preprocessing 中使用

MIT/Apache

240KB
4.5K SLoC

朴素贝叶斯

linfa-bayes 为 Linfa 工具箱提供纯 Rust 实现的朴素贝叶斯算法。

总体概览

linfa-bayes 是 Linfa 生态系统中的一个 crate,旨在创建一个纯 Rust 实现的经典机器学习工具箱,类似于 Python 的 scikit-learn

当前状态

linfa-bayes 目前提供以下方法的实现

示例

您可以在 examples/ 目录中找到示例。要运行高斯朴素贝叶斯示例,请使用

$ 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(())

要运行多项式朴素贝叶斯示例,请使用

$ 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(())

依赖项

~4.5MB
~92K SLoC