BernoulliNaiveBayes<T>
Models & Types · AiDotNet.Classification.NaiveBayes
Bernoulli Naive Bayes classifier for binary/boolean features.
Inheritance: ClassifierBase<T> → ProbabilisticClassifierBase<T> → NaiveBayesBase<T> → BernoulliNaiveBayes<T>
For Beginners
This classifier works best with yes/no, true/false, or present/absent data:
- Does the document contain this word? (yes/no)
- Is this feature present? (0 or 1)
- Does the user have this attribute?
The key difference from Multinomial NB is that Bernoulli NB cares about absence - it penalizes when a feature that’s usually present for a class is absent in the sample.
Example use cases:
- Document classification with binary word presence (not counts)
- Spam detection with binary features
- Any classification with boolean attributes
How It Works
Bernoulli Naive Bayes is suitable for classification with binary features (features that are either 0 or 1, present or absent). It models each feature as a Bernoulli distribution.
Unlike Multinomial Naive Bayes, Bernoulli NB explicitly models the absence of features, making it suitable for problems where “not having” a feature is informative.
Example
// Create Bernoulli Naive Bayes for binary feature classification
var options = new NaiveBayesOptions<double>();
// Prepare binary feature data (0 = absent, 1 = present)
var features = new Matrix<double>(4, 3);
features[0, 0] = 1; features[0, 1] = 1; features[0, 2] = 0; // Class 0
features[1, 0] = 1; features[1, 1] = 0; features[1, 2] = 0; // Class 0
features[2, 0] = 0; features[2, 1] = 1; features[2, 2] = 1; // Class 1
features[3, 0] = 0; features[3, 1] = 0; features[3, 2] = 1; // Class 1
var labels = new Vector<double>(new double[] { 0, 0, 1, 1 });
// Train by learning feature presence/absence probabilities per class
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
.ConfigureModel(new BernoulliNaiveBayes<double>(options))
.Build(features, labels);
// Predict using Bernoulli likelihood with absence penalty
var newSample = new Matrix<double>(1, 3);
newSample[0, 0] = 1; newSample[0, 1] = 1; newSample[0, 2] = 0;
var prediction = result.Predict(newSample);
// Result is available in the returned value
Constructors
| Constructor | Summary |
|---|---|
BernoulliNaiveBayes(NaiveBayesOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization, double binarizeThreshold) |
Initializes a new instance of the BernoulliNaiveBayes class. |
Methods
| Method | Summary |
|---|---|
ModelMetadata<T> GetModelMetadata() |