Class BernoulliNaiveBayes<T>
- Namespace
- AiDotNet.Classification.NaiveBayes
- Assembly
- AiDotNet.dll
Bernoulli Naive Bayes classifier for binary/boolean features.
public class BernoulliNaiveBayes<T> : NaiveBayesBase<T>, IProbabilisticClassifier<T>, IClassifier<T>, IFullModel<T, Matrix<T>, Vector<T>>, IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Matrix<T>, Vector<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>, IGradientComputable<T, Matrix<T>, Vector<T>>, IJitCompilable<T>
Type Parameters
TThe numeric data type used for calculations (e.g., float, double).
- Inheritance
-
BernoulliNaiveBayes<T>
- Implements
-
IClassifier<T>
- Inherited Members
- Extension Methods
Remarks
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.
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
Constructors
BernoulliNaiveBayes(NaiveBayesOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?, double)
Initializes a new instance of the BernoulliNaiveBayes class.
public BernoulliNaiveBayes(NaiveBayesOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null, double binarizeThreshold = 0)
Parameters
optionsNaiveBayesOptions<T>Configuration options for the Naive Bayes classifier.
regularizationIRegularization<T, Matrix<T>, Vector<T>>Optional regularization strategy.
binarizeThresholddoubleThreshold for binarizing features. Default is 0.0.
Methods
Clone()
Creates a deep clone of this model.
public override IFullModel<T, Matrix<T>, Vector<T>> Clone()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A cloned BernoulliNaiveBayes instance.
ComputeClassParameters(Matrix<T>, Vector<T>)
Computes the log feature probabilities for presence and absence for each class.
protected override void ComputeClassParameters(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>The input features matrix (will be binarized internally).
yVector<T>The target class labels vector.
Remarks
For Bernoulli Naive Bayes, the probability of feature f being present given class c is: P(f=1|c) = (count of samples with f=1 in class c + alpha) / (count of samples in class c + 2*alpha)
where alpha is the smoothing parameter.
ComputeLogLikelihood(Vector<T>, int)
Computes the log-likelihood of a sample given a class using Bernoulli distribution.
protected override T ComputeLogLikelihood(Vector<T> sample, int classIndex)
Parameters
sampleVector<T>The feature vector for a single sample.
classIndexintThe class index.
Returns
- T
The log-likelihood log P(sample|class).
Remarks
For Bernoulli Naive Bayes, the log-likelihood accounts for both presence and absence: log P(x|c) = sum over all features of: if feature is present: log P(f=1|c) if feature is absent: log P(f=0|c)
CreateNewInstance()
Creates a new instance of this model type.
protected override IFullModel<T, Matrix<T>, Vector<T>> CreateNewInstance()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new BernoulliNaiveBayes instance.
GetModelMetadata()
Gets metadata about the model.
public override ModelMetadata<T> GetModelMetadata()
Returns
- ModelMetadata<T>
A ModelMetadata object containing information about the model.
Remarks
This method returns metadata about the model, including its type, feature count, complexity, description, and additional information specific to classification.
For Beginners: Model metadata provides information about the model itself, rather than the predictions it makes. This includes details about the model's structure (like how many features it uses) and characteristics (like how many classes it can predict). This information can be useful for understanding and comparing different models.
GetModelType()
Returns the model type identifier for this classifier.
protected override ModelType GetModelType()
Returns
- ModelType
ModelType.BernoulliNaiveBayes