GaussianNB
Gaussian Naive Bayes classifier.
Implements the Gaussian Naive Bayes algorithm for classification. Assumes features follow a Gaussian (normal) distribution.
Algorithm:
- Calculate mean and variance for each feature per class
- For prediction, calculate likelihood using Gaussian PDF
- Apply Bayes’ theorem to get posterior probabilities
- Predict class with highest posterior probability
Time Complexity:
- Training: O(n * d) where n=samples, d=features
- Prediction: O(k * d) per sample where k=classes
Constructor
new GaussianNB(options?: {
varSmoothing?: number;
})
Portion of largest variance added to variances for stability. Helps avoid degenerate Gaussians when variance is zero.
Methods
fit
fit(X: Tensor, y: Tensor): this
Fit Gaussian Naive Bayes classifier from the training set.
Computes per-class mean, variance, and prior probabilities.
Training data of shape (n_samples, n_features)
Target class labels of shape (n_samples,)
Returns: The fitted estimator
Throws: DataValidationError if zero variance encountered with varSmoothing=0
predict
predict(X: Tensor): Tensor
Predict class labels for samples in X.
Samples of shape (n_samples, n_features)
Returns: Predicted class labels of shape (n_samples,)
predictProba
predictProba(X: Tensor): Tensor
Predict class probabilities for samples in X.
Uses Bayes’ theorem with Gaussian class-conditional likelihoods.
Samples of shape (n_samples, n_features)
Returns: Class probability matrix of shape (n_samples, n_classes)
score
score(X: Tensor, y: Tensor): number
Return the mean accuracy on the given test data and labels.
Test samples of shape (n_samples, n_features)
True labels of shape (n_samples,)
Returns: Accuracy score in range [0, 1]
Properties
Unique class labels discovered during fitting.
Example
import { GaussianNB } from 'deepbox/ml';
import { tensor } from 'deepbox/ndarray';
const X = tensor([[1, 2], [2, 3], [3, 4], [4, 5]]);
const y = tensor([0, 0, 1, 1]);
const nb = new GaussianNB();
nb.fit(X, y);
const predictions = nb.predict(tensor([[2.5, 3.5]]));
const probabilities = nb.predictProba(tensor([[2.5, 3.5]]));