GaussianNaiveBayes

Struct GaussianNaiveBayes 

Source
pub struct GaussianNaiveBayes<State = Unfit> {
    pub classes: Vec<String>,
    pub probability_of_class: HashMap<String, f64>,
    pub probability_of_feat_by_class: HashMap<String, Vec<(f64, f64)>>,
    /* private fields */
}
Expand description

The GaussianNaiveBayes struct represents a Gaussian Naive Bayes classifier.

A Gaussian Naive Bayes classifier is a type of probabilistic machine learning model used for classification tasks. The Gaussian version assumes the features that it is learning from are distributed normally.

This struct has two possible states: Unfit and Fit. An Unfit model is one that has not yet been trained on data, while a Fit model has been trained and can be used for making predictions.

§Fields

  • classes - A vector of unique class labels (targets) that the model may predict.

  • probability_of_class - A hashmap where keys are the class labels and the values are the corresponding prior probabilities of each class.

  • probability_of_feat_by_class - A hashmap where keys are the class labels and the values are vectors of tuples. Each tuple represents the mean and standard deviation of a particular feature for that class.

  • state - A marker for the model’s state. This is either Unfit (for a newly instantiated model) or Fit (for a model that has been trained on data).

§Examples

use std::collections::HashMap;
use ducky_learn::naive_bayes::GaussianNaiveBayes;

let model = GaussianNaiveBayes::new();

let x_train: Vec<Vec<f64>> = vec![
    vec![1.0, 2.0],
    vec![2.0, 3.0],
    vec![3.0, 4.0],
    vec![4.0, 5.0],
];
let y_train: Vec<String> = vec![
    "class1".to_string(),
    "class2".to_string(),
    "class1".to_string(),
    "class2".to_string(),
];

let model = model.fit(&x_train, &y_train);

let x_test: Vec<Vec<f64>> = vec![
    vec![1.5, 2.5],
    vec![3.5, 4.5],
];

let predictions = model.predict(&x_test);

println!("{:?}", predictions);

Fields§

§classes: Vec<String>§probability_of_class: HashMap<String, f64>§probability_of_feat_by_class: HashMap<String, Vec<(f64, f64)>>

Implementations§

Source§

impl GaussianNaiveBayes

Source

pub fn new() -> Self

Creates a new GaussianNaiveBayes instance with an Unfit state.

§Returns
  • Self - A new instance of GaussianNaiveBayes.
§Examples
use ducky_learn::naive_bayes::GaussianNaiveBayes;
let model = GaussianNaiveBayes::new();
Source

pub fn fit(self, x: &Vec<Vec<f64>>, y: &Vec<String>) -> GaussianNaiveBayes<Fit>

Fits the model on the provided dataset, updating the model’s state to Fit.

§Arguments
  • x - A reference to a vector of vectors, where each inner vector represents the features of a data point.

  • y - A reference to a vector of class labels for each data point in x.

§Returns
  • GaussianNaiveBayes<Fit> - The same model instance with updated fields and state set to Fit.
§Examples
use ducky_learn::naive_bayes::GaussianNaiveBayes;

let model = GaussianNaiveBayes::new();

let x_train: Vec<Vec<f64>> = vec![
    vec![1.0, 2.0],
    vec![2.0, 3.0],
    vec![3.0, 4.0],
    vec![4.0, 5.0],
];
let y_train: Vec<String> = vec![
    "class1".to_string(),
    "class2".to_string(),
    "class1".to_string(),
    "class2".to_string(),
];

let model = model.fit(&x_train, &y_train);
Source§

impl GaussianNaiveBayes<Fit>

Source

pub fn predict(&self, x: &Vec<Vec<f64>>) -> Vec<String>

Predicts the class of the provided data points.

§Arguments
  • x - A reference to a vector of vectors, where each inner vector represents the features of a data point.
§Returns
  • Vec<String> - A vector of predicted class labels for each data point in x.
§Examples
use ducky_learn::naive_bayes::GaussianNaiveBayes;

let model = GaussianNaiveBayes::new().fit(
    &vec![vec![0.1, 0.5], vec![0.6, 0.6]],
    &vec!["class1".to_string(), "class2".to_string()]
);

let x_test: Vec<Vec<f64>> = vec![
    vec![1.5, 2.5],
    vec![3.5, 4.5],
];

let predictions = model.predict(&x_test);

println!("{:?}", predictions);

Auto Trait Implementations§

§

impl<State> Freeze for GaussianNaiveBayes<State>

§

impl<State> RefUnwindSafe for GaussianNaiveBayes<State>
where State: RefUnwindSafe,

§

impl<State> Send for GaussianNaiveBayes<State>
where State: Send,

§

impl<State> Sync for GaussianNaiveBayes<State>
where State: Sync,

§

impl<State> Unpin for GaussianNaiveBayes<State>
where State: Unpin,

§

impl<State> UnwindSafe for GaussianNaiveBayes<State>
where State: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V