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 beFitorUnfit.
§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_testFields§
§alpha: f64§probability_of_class: HashMap<String, f64>§probability_of_feat_by_class: HashMap<String, HashMap<String, f64>>Implementations§
Trait Implementations§
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> 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
Mutably borrows from an owned value. Read more