Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gaussian Naive Bayes (GaussianNB) #51

Merged
merged 15 commits into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ members = [
"linfa-svm",
"linfa-hierarchical",
"linfa-ica",
"linfa-bayes",
"datasets",
]

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Where does `linfa` stand right now? [Are we learning yet?](http://www.arewelearn
| [trees](linfa-trees/) | Decision trees | Experimental | Supervised learning | Linear decision trees
| [svm](linfa-svm/) | Support Vector Machines | Tested | Supervised learning | Classification or regression analysis of labeled datasets |
| [hierarchical](linfa-hierarchical/) | Agglomerative hierarchical clustering | Tested | Unsupervised learning | Cluster and build hierarchy of clusters |
| [bayes](linfa-bayes/) | Naive Bayes | Tested | Supervised learning | Contain's Gaussian Naive Bayes |

We believe that only a significant community effort can nurture, build, and sustain a machine learning ecosystem in Rust - there is no other way forward.

Expand Down
21 changes: 21 additions & 0 deletions linfa-bayes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "linfa-bayes"
version = "0.1.0"
authors = ["VasanthakumarV <vasanth260m12@gmail.com>"]
description = "Collection of Naive Bayes Algorithms"
edition = "2018"
license = "MIT/Apache-2.0"
repository = "https://github.com/rust-ml/linfa"
readme = "README.md"
keywords = ["factorization", "machine-learning", "linfa", "unsupervised"]
categories = ["algorithms", "mathematics", "science"]

[dependencies]
ndarray = { version = "0.13" , features = ["blas", "approx"]}
ndarray-stats = "0.3"
linfa = { version = "0.2.1", path = ".." }

[dev-dependencies]
approx = "0.3"
linfa = { path = ".." }
linfa-datasets = { version = "0.2.0", path = "../datasets", features = ["winequality"] }
27 changes: 27 additions & 0 deletions linfa-bayes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Naive Bayes
bytesnake marked this conversation as resolved.
Show resolved Hide resolved

`linfa-bayes` aims to provide pure Rust implementations of Naive Bayes algorithms.

## The Big Picture

`linfa-bayes` is a crate in the [`linfa`](https://crates.io/crates/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)

## Examples

There is an usage example in the `examples/` directory. To run, use:

```bash
$ cargo run --example winequality
```

## License
Dual-licensed to be compatible with the Rust project.

Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be copied, modified, or distributed except according to those terms.

39 changes: 39 additions & 0 deletions linfa-bayes/examples/winequality.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::error::Error;

use linfa::metrics::ToConfusionMatrix;
use linfa::traits::{Fit, Predict};
use linfa_bayes::GaussianNbParams;

fn main() -> Result<(), Box<dyn Error>> {
// Wine with rating greater than 6 is considered good
fn tag_classes(x: &usize) -> usize {
if *x > 6 {
1
} else {
0
}
};

// Read in the dataset and convert continuous target into categorical
let data = linfa_datasets::winequality().map_targets(tag_classes);

let (train, valid) = data.split_with_ratio_view(0.9);

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

let pred = model.predict(valid.records);

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

// classes | 1 | 0
// 1 | 10 | 12
// 0 | 7 | 130
//
// accuracy 0.8805031, MCC 0.45080978
println!("{:?}", cm);
println!("accuracy {}, MCC {}", cm.accuracy(), cm.mcc());

Ok(())
}
27 changes: 27 additions & 0 deletions linfa-bayes/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::fmt;

use ndarray_stats::errors::MinMaxError;

pub type Result<T> = std::result::Result<T, BayesError>;

#[derive(Debug)]
pub enum BayesError {
bytesnake marked this conversation as resolved.
Show resolved Hide resolved
/// Error when performing Max operation on data
Stats(MinMaxError),
}

impl fmt::Display for BayesError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Stats(error) => write!(f, "Ndarray Stats Error: {}", error),
}
}
}

impl From<MinMaxError> for BayesError {
fn from(error: MinMaxError) -> Self {
Self::Stats(error)
}
}

impl std::error::Error for BayesError {}
Loading