Skip to main content

GaussianNbModel

Struct GaussianNbModel 

Source
pub struct GaussianNbModel { /* private fields */ }
Expand description

Fitted Gaussian Naive Bayes model.

Holds the sorted class labels, the per-class log priors, and the per-class per-feature Gaussian mean and (smoothed) variance. Construct one with gaussian_nb_fit.

Implementations§

Source§

impl GaussianNbModel

Source

pub fn classes(&self) -> &[usize]

Returns the sorted class labels, in Self::predict_log_proba column order.

Source

pub fn predict(&self, x: &[Vec<f64>]) -> Result<Vec<usize>>

Predicts the class label for every sample in x (ties break low).

§Arguments
  • x — rows to classify; each must have the fitted feature count.
§Returns

One predicted class label per input row, in input order.

§Errors

Error::InvalidInput if any row’s length differs from the fitted feature count.

§Examples
use stats_claw::algorithms::classification::naive_bayes::gaussian_nb_fit;

let x = vec![vec![0.0], vec![0.5], vec![9.0], vec![9.5]];
let y = vec![0, 0, 1, 1];
let model = gaussian_nb_fit(&x, &y)?;
assert_eq!(model.predict(&[vec![9.1]])?, vec![1]);
Source

pub fn predict_log_proba(&self, x: &[Vec<f64>]) -> Result<Vec<Vec<f64>>>

Predicts the normalized log posteriors for every sample in x.

Each returned row has one entry per class (in Self::classes order) whose exponentials sum to 1.

§Arguments
  • x — rows to score; each must have the fitted feature count.
§Returns

One log-posterior row per input row.

§Errors

Error::InvalidInput if any row’s length differs from the fitted feature count.

§Examples
use stats_claw::algorithms::classification::naive_bayes::gaussian_nb_fit;

let x = vec![vec![0.0], vec![0.5], vec![9.0], vec![9.5]];
let y = vec![0, 0, 1, 1];
let model = gaussian_nb_fit(&x, &y)?;
let lp = model.predict_log_proba(&[vec![0.1]])?;
let total: f64 = lp[0].iter().map(|v| v.exp()).sum();
assert!((total - 1.0).abs() < 1e-12, "posteriors summed to {total}");
Source

pub fn classification_result( &self, x: &[Vec<f64>], y_true: &[usize], ) -> Result<ClassificationResult>

Builds a populated ClassificationResult from predictions on (x, y_true): accuracy plus macro-averaged precision / recall / F1.

§Arguments
  • x — evaluation design matrix.
  • y_true — the true class labels, one per row of x.
§Returns

A ClassificationResult scored on (x, y_true).

§Errors
§Examples
use stats_claw::algorithms::classification::naive_bayes::gaussian_nb_fit;

let x = vec![vec![0.0], vec![0.5], vec![9.0], vec![9.5]];
let y = vec![0, 0, 1, 1];
let model = gaussian_nb_fit(&x, &y)?;
let result = model.classification_result(&x, &y)?;
assert!((result.accuracy - 1.0).abs() < 1e-12, "accuracy {}", result.accuracy);

Trait Implementations§

Source§

impl Clone for GaussianNbModel

Source§

fn clone(&self) -> GaussianNbModel

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GaussianNbModel

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.