Table of Contents

Class MultinomialNaiveBayes<T>

Namespace
AiDotNet.Classification.NaiveBayes
Assembly
AiDotNet.dll

Multinomial Naive Bayes classifier for discrete count data.

public class MultinomialNaiveBayes<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

T

The numeric data type used for calculations (e.g., float, double).

Inheritance
MultinomialNaiveBayes<T>
Implements
IFullModel<T, Matrix<T>, Vector<T>>
IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>
IParameterizable<T, Matrix<T>, Vector<T>>
ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>
IGradientComputable<T, Matrix<T>, Vector<T>>
Inherited Members
Extension Methods

Remarks

Multinomial Naive Bayes is suitable for classification with discrete features representing counts or frequencies (e.g., word counts in text classification). It models each feature as a multinomial distribution.

For Beginners: This classifier works best with count data - things you can count, like: - Number of times each word appears in a document (text classification) - Number of each type of event - Frequency of features in categorical data

It's the go-to algorithm for spam detection and sentiment analysis!

During training, it learns how often each feature occurs in each class. During prediction, it calculates which class is most likely given the observed feature counts.

Example use cases:

  • Spam detection (word counts in emails)
  • Topic classification of documents
  • Sentiment analysis (positive/negative word counts)

Constructors

MultinomialNaiveBayes(NaiveBayesOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?)

Initializes a new instance of the MultinomialNaiveBayes class.

public MultinomialNaiveBayes(NaiveBayesOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)

Parameters

options NaiveBayesOptions<T>

Configuration options for the Naive Bayes classifier.

regularization IRegularization<T, Matrix<T>, Vector<T>>

Optional regularization strategy.

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 MultinomialNaiveBayes instance.

ComputeClassParameters(Matrix<T>, Vector<T>)

Computes the log feature probabilities for each class using Laplace smoothing.

protected override void ComputeClassParameters(Matrix<T> x, Vector<T> y)

Parameters

x Matrix<T>

The input features matrix (should contain non-negative counts).

y Vector<T>

The target class labels vector.

Remarks

For multinomial Naive Bayes, the probability of feature f given class c is: P(f|c) = (count(f,c) + alpha) / (sum of all feature counts for c + alpha * n_features)

where alpha is the smoothing parameter (Laplace smoothing when alpha=1).

ComputeLogLikelihood(Vector<T>, int)

Computes the log-likelihood of a sample given a class using multinomial distribution.

protected override T ComputeLogLikelihood(Vector<T> sample, int classIndex)

Parameters

sample Vector<T>

The feature vector for a single sample (should be non-negative counts).

classIndex int

The class index.

Returns

T

The log-likelihood log P(sample|class).

Remarks

For multinomial Naive Bayes, the log-likelihood is: log P(x|c) = sum over all features of: x_f * log P(f|c)

where x_f is the count for feature f in the sample.

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 MultinomialNaiveBayes 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.MultinomialNaiveBayes