Naive Bayes classifier implementation.
Classes
Naive Bayes classifier using pluggable probability estimators.
__init__( self, use_kernel_estimator: bool = False, use_laplace: bool = True, var_smoothing: float = 1e-09, )
Overview
The algorithm classifies instances through the following steps:
- Compute the prior probability of each class from training labels
- For each feature, fit a probability estimator (Gaussian or KDE) per class
- At prediction time, compute the posterior for each class as the
- Return the class with the highest posterior probability
Theory
Classification is based on Bayes' theorem. The posterior probability of class c given a feature vector \mathbf{x} is:
Under the naive independence assumption, the likelihood factorises:
so the decision rule becomes:
Each factor P(x_i \mid c) is estimated by a pluggable density estimator -- either a Gaussian (normal) distribution or a kernel density estimator (KDE).
Parameters
use_kernel_estimator
True, use kernel density estimation for numeric attributes instead of assuming a Gaussian distribution. Kernel estimation is more flexible but computationally more expensive.
use_laplace
True, apply Laplace (add-one) smoothing to class priors. This prevents zero probabilities for classes with few samples.
Attributes
classes_
fit.
class_prior_
P(class). Computed from training data, optionally with Laplace smoothing.
estimators_
estimators_[class_idx][feature_idx]. Each estimator models the conditional distribution P(feature|class).
Notes
Complexity:
- Training: O(n \cdot m \cdot c) where n = samples, m = features, c = classes
- Prediction: O(m \cdot c) per sample (Gaussian); O(n \cdot m \cdot c) per sample (KDE)
- When you need a fast, simple baseline classifier
- Text classification and spam filtering
- When features are approximately independent given the class
- Datasets with missing values (NaN values are handled gracefully)
- When interpretable class probabilities are desired
use_kernel_estimator=True for non-Gaussian or multimodal data.References
See Also
Basic classification with default settings:
>>> from tuiml.algorithms.bayesian import NaiveBayesClassifier
>>> import numpy as np
>>>
>>> # Create sample training data
>>> X_train = np.array([[1, 2], [2, 3], [3, 4], [6, 7], [7, 8], [8, 9]])
>>> y_train = np.array([0, 0, 0, 1, 1, 1])
>>>
>>> # Fit and predict with Gaussian estimators
>>> clf = NaiveBayesClassifier()
>>> clf.fit(X_train, y_train)
NaiveBayesClassifier(classes=[0, 1], estimator=Normal)
>>> clf.predict([[2, 3]])
array([0])
>>> clf.predict_proba([[2, 3]]) # doctest: +SKIP
array([[0.99, 0.01]])
Using kernel density estimation for non-Gaussian data:
>>> clf_kde = NaiveBayesClassifier(use_kernel_estimator=True)
>>> clf_kde.fit(X_train, y_train)
NaiveBayesClassifier(classes=[0, 1], estimator=Kernel)
Methods
get_capabilities
(cls) -> List[str]
get_capabilities
(cls) -> List[str]
Return the data capabilities supported by this classifier.
Returns
list of str
List of capability identifiers:
- •
"numeric": Handles numeric/continuous features - •
"missing_values": Handles missing values (NaN) - •
"binary_class": Supports binary classification - •
"multiclass": Supports multi-class classification
fit
(self, X: np.ndarray, y: np.ndarray) -> 'NaiveBayesClassifier'
fit
(self, X: np.ndarray, y: np.ndarray) -> 'NaiveBayesClassifier'
Fit the Naive Bayes classifier to the training data.
Parameters
X
y
Returns
self
partial_fit
(self, X: np.ndarray, y: np.ndarray, classes: Optional[np.ndarray]=None) -> 'NaiveBayesClassifier'
partial_fit
(self, X: np.ndarray, y: np.ndarray, classes: Optional[np.ndarray]=None) -> 'NaiveBayesClassifier'
Incrementally fit the Naive Bayes classifier on a batch of samples.
Parameters
X
y
classes
Returns
self
predict
(self, X: np.ndarray) -> np.ndarray
predict
(self, X: np.ndarray) -> np.ndarray
Predict class labels for input samples.
Parameters
X
Returns
numpy.ndarray of shape (n_samples,)
Raises
RuntimeError
predict_proba
(self, X: np.ndarray) -> np.ndarray
predict_proba
(self, X: np.ndarray) -> np.ndarray
Predict class membership probabilities for input samples.
Parameters
X
Returns
numpy.ndarray of shape (n_samples, n_classes)
self.classes_. Each row sums to 1.0.
Raises
RuntimeError
predict_log_proba
(self, X: np.ndarray) -> np.ndarray
predict_log_proba
(self, X: np.ndarray) -> np.ndarray
Predict log-probabilities of class membership for input samples.
Parameters
X
Returns
numpy.ndarray of shape (n_samples, n_classes)
self.classes_.
Raises
RuntimeError