Multinomial Naive Bayes classifier implementation.
Classes
class algorithms.bayesian.naive_bayes_multinomial.NaiveBayesMultinomialClassifier(Classifier)
Multinomial Naive Bayes classifier for text and discrete data.
__init__( self, alpha: float = 1.0, )
Overview
The algorithm classifies documents through the following steps:
- Count the frequency of each feature (word) for every class
- Apply additive smoothing to avoid zero probabilities
- Compute log class priors and log feature likelihoods
- At prediction time, compute the joint log-likelihood and return
Theory
The multinomial model assumes features are generated from a multinomial distribution. The posterior for class c given document \mathbf{x} = (x_1, \ldots, x_m) is:
where x_i is the count of feature i in the document.
With Laplace smoothing (parameter \alpha), the feature likelihood is estimated as:
where N_{ic} is the total count of feature i in class c, N_c is the total count of all features in class c, and m is the vocabulary size.
Parameters
alpha
Attributes
classes_
fit.
class_prior_
log P(class).
feature_log_prob_
log P(feature | class).
class_count_
feature_count_
Notes
Complexity:
- Training: O(n \cdot m) where n = samples, m = features
- Prediction: O(m \cdot c) per sample where c = classes
- Text classification (e.g., spam filtering, sentiment analysis)
- Features are word counts, term frequencies, or TF-IDF values
- High-dimensional sparse data (large vocabularies)
-
Online / incremental learning via
partial_fit
References
Basic text-like classification with word counts:
>>> from tuiml.algorithms.bayesian import NaiveBayesMultinomialClassifier
>>> import numpy as np
>>>
>>> # Word count features for 4 documents
>>> X = np.array([[2, 1, 0], [1, 2, 0], [0, 1, 2], [0, 2, 1]])
>>> y = np.array([0, 0, 1, 1])
>>>
>>> # Fit with Laplace smoothing
>>> clf = NaiveBayesMultinomialClassifier(alpha=1.0)
>>> clf.fit(X, y)
NaiveBayesMultinomialClassifier(alpha=1.0, classes=[0, 1])
>>> clf.predict([[3, 0, 0]])
array([0])
Methods
fit
(self, X: np.ndarray, y: np.ndarray) -> 'NaiveBayesMultinomialClassifier'
fit
(self, X: np.ndarray, y: np.ndarray) -> 'NaiveBayesMultinomialClassifier'
Fit the Multinomial Naive Bayes classifier to the training data.
Parameters
X
n_samples is the number of samples and n_features is the number of features. Features should be non-negative (frequencies, counts, or TF-IDF).
y
Returns
self
predict_proba
(self, X: np.ndarray) -> np.ndarray
predict_proba
(self, X: np.ndarray) -> np.ndarray
Return probability estimates for the test vectors X.
Parameters
X
Returns
C
self.classes_.
predict_log_proba
(self, X: np.ndarray) -> np.ndarray
predict_log_proba
(self, X: np.ndarray) -> np.ndarray
Predict log class probabilities for samples.
Parameters
X
Returns
log_proba
self.classes_.
partial_fit
(self, X: np.ndarray, y: np.ndarray, classes: Optional[np.ndarray]=None) -> 'NaiveBayesMultinomialClassifier'
partial_fit
(self, X: np.ndarray, y: np.ndarray, classes: Optional[np.ndarray]=None) -> 'NaiveBayesMultinomialClassifier'
Incremental fit on a batch of samples.
Parameters
X
n_samples is the number of samples and n_features is the number of features.
y
classes
Returns
self