StdNaiveBayes

Struct StdNaiveBayes 

Source
pub struct StdNaiveBayes<State = Unfit> {
    pub alpha: f64,
    pub probability_of_class: HashMap<String, f64>,
    pub probability_of_feat_by_class: HashMap<String, HashMap<String, f64>>,
    /* private fields */
}
Expand description

Implementation of a standard Naive Bayes classifier.

This classifier uses Laplace smoothing, the degree of which can be controlled with the alpha parameter.

§Parameters

  • alpha: The Laplace smoothing factor.
  • probability_of_class: HashMap storing the probabilities of each class.
  • probability_of_feat_by_class: HashMap storing the probabilities of each feature given a class.
  • state: PhantomData indicating whether the classifier has been fit.

§Type parameters

  • State: Indicates whether the classifier has been fit. Can either be Fit or Unfit.

§Example

use ducky_learn::naive_bayes::StdNaiveBayes;

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

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

let mut nb = StdNaiveBayes::new(1.0);
let nb = nb.fit(&x_train, &y_train);
let y_pred = nb.predict(&x_test);

// y_pred will hold the predicted classes for x_test

Fields§

§alpha: f64§probability_of_class: HashMap<String, f64>§probability_of_feat_by_class: HashMap<String, HashMap<String, f64>>

Implementations§

Source§

impl StdNaiveBayes

Source

pub fn new(alpha: f64) -> Self

Constructs a new, unfitted StdNaiveBayes classifier with a specified alpha value.

§Parameters
  • alpha: The Laplace smoothing factor.
§Returns

A new StdNaiveBayes instance.

Source

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

Fits the StdNaiveBayes classifier to the training data.

§Parameters
  • x: The training data.
  • y: The target values.
§Returns

The fitted StdNaiveBayes classifier.

Source§

impl StdNaiveBayes<Fit>

Source

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

Predicts the target values for the given data.

§Parameters
  • x: The data to predict target values for.
§Returns

The predicted target values.

§Panics

This function will panic if the classifier has not been fit.

Trait Implementations§

Source§

impl<State: Debug> Debug for StdNaiveBayes<State>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<State> Freeze for StdNaiveBayes<State>

§

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

§

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

§

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

§

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

§

impl<State> UnwindSafe for StdNaiveBayes<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