pub struct GaussianNB<XT: RealNumber, YT: WholeNumber> { /* private fields */ }Expand description
Implementation of Gaussian Naive Bayes classifier.
This struct represents a Gaussian Naive Bayes classifier. It is used to fit a training dataset and make predictions on new data points. The classifier assumes that the features are independent and follow a Gaussian distribution.
§Type Parameters
XT: The type of the input features.YT: The type of the target labels.
§Example
use rusty_ai::bayes::gaussian::GaussianNB;
use rusty_ai::data::dataset::Dataset;
use nalgebra::{DMatrix, DVector};
// Create a new Gaussian Naive Bayes classifier
let mut classifier = GaussianNB::new();
// Create a training dataset
let x = DMatrix::from_row_slice(3, 2, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
let y = DVector::from_vec(vec![0, 1, 0]);
let dataset = Dataset::new(x, y);
// Fit the classifier to the training dataset
let result = classifier.fit(&dataset);
assert!(result.is_ok());
// Make predictions on new data points
let x_test = DMatrix::from_row_slice(2, 2, &[1.0, 2.0, 3.0, 4.0]);
let predictions = classifier.predict(&x_test);
assert!(predictions.is_ok());Implementations§
Source§impl<XT: RealNumber, YT: WholeNumber> GaussianNB<XT, YT>
impl<XT: RealNumber, YT: WholeNumber> GaussianNB<XT, YT>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new Gaussian Naive Bayes classifier.
This function initializes the classifier with empty class frequency, mean, and variance maps.
§Returns
A new instance of GaussianNB.
Sourcepub fn class_freq(&self) -> &HashMap<YT, XT>
pub fn class_freq(&self) -> &HashMap<YT, XT>
Returns a reference to the class frequency map.
This function returns a reference to the map that stores the frequency of each class in the training dataset.
§Returns
A reference to the class frequency map.
Sourcepub fn class_mean(&self) -> &HashMap<YT, DVector<XT>>
pub fn class_mean(&self) -> &HashMap<YT, DVector<XT>>
Returns a reference to the class mean map.
This function returns a reference to the map that stores the mean values of each feature for each class in the training dataset.
§Returns
A reference to the class mean map.
Sourcepub fn class_variance(&self) -> &HashMap<YT, DVector<XT>>
pub fn class_variance(&self) -> &HashMap<YT, DVector<XT>>
Returns a reference to the class variance map.
This function returns a reference to the map that stores the variance values of each feature for each class in the training dataset.
§Returns
A reference to the class variance map.
Sourcepub fn fit(
&mut self,
dataset: &Dataset<XT, YT>,
) -> Result<String, Box<dyn Error>>
pub fn fit( &mut self, dataset: &Dataset<XT, YT>, ) -> Result<String, Box<dyn Error>>
Fits the classifier to a training dataset.
This function fits the classifier to the provided training dataset. It calculates the class frequency, mean, and variance for each class in the dataset.
§Arguments
dataset- The training dataset to fit the classifier to.
§Returns
A Result indicating whether the fitting process was successful or an error occurred.
Sourcepub fn predict(&self, x: &DMatrix<XT>) -> Result<DVector<YT>, Box<dyn Error>>
pub fn predict(&self, x: &DMatrix<XT>) -> Result<DVector<YT>, Box<dyn Error>>
Predicts the class labels for a given matrix of feature vectors.
This function predicts the class labels for each feature vector in the input matrix using the fitted Gaussian Naive Bayes classifier. It returns a vector of predicted class labels.
§Arguments
x- The matrix of feature vectors to predict the class labels for.
§Returns
A Result containing a vector of predicted class labels or an error if the prediction
process fails.
Trait Implementations§
Source§impl<XT: RealNumber, YT: WholeNumber> ClassificationMetrics<YT> for GaussianNB<XT, YT>
impl<XT: RealNumber, YT: WholeNumber> ClassificationMetrics<YT> for GaussianNB<XT, YT>
Source§fn confusion_matrix(
&self,
y_true: &DVector<T>,
y_pred: &DVector<T>,
) -> Result<DMatrix<usize>, Box<dyn Error>>
fn confusion_matrix( &self, y_true: &DVector<T>, y_pred: &DVector<T>, ) -> Result<DMatrix<usize>, Box<dyn Error>>
Source§fn accuracy(
&self,
y_true: &DVector<T>,
y_pred: &DVector<T>,
) -> Result<f64, Box<dyn Error>>
fn accuracy( &self, y_true: &DVector<T>, y_pred: &DVector<T>, ) -> Result<f64, Box<dyn Error>>
Source§fn precision(
&self,
y_true: &DVector<T>,
y_pred: &DVector<T>,
) -> Result<f64, Box<dyn Error>>
fn precision( &self, y_true: &DVector<T>, y_pred: &DVector<T>, ) -> Result<f64, Box<dyn Error>>
Source§impl<XT: Clone + RealNumber, YT: Clone + WholeNumber> Clone for GaussianNB<XT, YT>
impl<XT: Clone + RealNumber, YT: Clone + WholeNumber> Clone for GaussianNB<XT, YT>
Source§fn clone(&self) -> GaussianNB<XT, YT>
fn clone(&self) -> GaussianNB<XT, YT>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<XT: Debug + RealNumber, YT: Debug + WholeNumber> Debug for GaussianNB<XT, YT>
impl<XT: Debug + RealNumber, YT: Debug + WholeNumber> Debug for GaussianNB<XT, YT>
Source§impl<XT: RealNumber, YT: WholeNumber> Default for GaussianNB<XT, YT>
impl<XT: RealNumber, YT: WholeNumber> Default for GaussianNB<XT, YT>
Auto Trait Implementations§
impl<XT, YT> Freeze for GaussianNB<XT, YT>
impl<XT, YT> RefUnwindSafe for GaussianNB<XT, YT>where
YT: RefUnwindSafe,
XT: RefUnwindSafe,
impl<XT, YT> Send for GaussianNB<XT, YT>
impl<XT, YT> Sync for GaussianNB<XT, YT>
impl<XT, YT> Unpin for GaussianNB<XT, YT>
impl<XT, YT> UnwindSafe for GaussianNB<XT, YT>where
YT: UnwindSafe,
XT: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.