pub struct NaiveBayesClassifier { /* private fields */ }Expand description
Multinomial Naïve Bayes text classifier with Laplace (additive) smoothing.
Internally works in log-space to prevent floating-point underflow.
§Example
use scirs2_text::text_classification::NaiveBayesClassifier;
let mut clf = NaiveBayesClassifier::new(1.0);
let texts = &["spam spam buy now", "hello friend good morning"];
let labels = &["spam", "ham"];
clf.fit(texts, labels).unwrap();
assert_eq!(clf.predict("buy now cheap").unwrap(), "spam");Implementations§
Source§impl NaiveBayesClassifier
impl NaiveBayesClassifier
Sourcepub fn new(alpha: f64) -> Self
pub fn new(alpha: f64) -> Self
Create a new classifier with smoothing parameter alpha.
alpha = 1.0 is the standard Laplace (add-one) smoothing.
§Errors
Returns TextError::InvalidInput when alpha <= 0.
Sourcepub fn fit(&mut self, texts: &[&str], labels: &[&str]) -> Result<()>
pub fn fit(&mut self, texts: &[&str], labels: &[&str]) -> Result<()>
Train the classifier.
§Errors
Returns TextError::InvalidInput when:
textsandlabelshave different lengths.- The corpus is empty.
alpha <= 0.
Sourcepub fn predict(&self, text: &str) -> Result<String>
pub fn predict(&self, text: &str) -> Result<String>
Predict the most probable class for text.
§Errors
Returns TextError::ModelNotFitted if fit has not been called.
Sourcepub fn predict_proba(&self, text: &str) -> Result<Vec<(String, f64)>>
pub fn predict_proba(&self, text: &str) -> Result<Vec<(String, f64)>>
Return (class_name, probability) pairs sorted by probability descending.
Probabilities are derived from log-scores via softmax normalisation.
§Errors
Returns TextError::ModelNotFitted if fit has not been called.
Auto Trait Implementations§
impl Freeze for NaiveBayesClassifier
impl RefUnwindSafe for NaiveBayesClassifier
impl Send for NaiveBayesClassifier
impl Sync for NaiveBayesClassifier
impl Unpin for NaiveBayesClassifier
impl UnsafeUnpin for NaiveBayesClassifier
impl UnwindSafe for NaiveBayesClassifier
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
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>
Converts
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>
Converts
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>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
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
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.