MultinomialNaiveBayes<T>
Models & Types · AiDotNet.Classification.NaiveBayes
Multinomial Naive Bayes classifier for discrete count data.
Inheritance: ClassifierBase<T> → ProbabilisticClassifierBase<T> → NaiveBayesBase<T> → MultinomialNaiveBayes<T>
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)
How It Works
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.
Example
// Create Multinomial Naive Bayes for text classification with word counts
var options = new NaiveBayesOptions<double>();
// Prepare word count features (bag-of-words representation)
var features = new Matrix<double>(4, 3);
features[0, 0] = 5; features[0, 1] = 1; features[0, 2] = 0; // Class 0: high word1
features[1, 0] = 3; features[1, 1] = 2; features[1, 2] = 0; // Class 0
features[2, 0] = 0; features[2, 1] = 1; features[2, 2] = 4; // Class 1: high word3
features[3, 0] = 0; features[3, 1] = 2; features[3, 2] = 3; // Class 1
var labels = new Vector<double>(new double[] { 0, 0, 1, 1 });
// Train by learning word frequency distributions per class
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
.ConfigureModel(new MultinomialNaiveBayes<double>(options))
.Build(features, labels);
// Predict class based on word count likelihoods
var newSample = new Matrix<double>(1, 3);
newSample[0, 0] = 4; newSample[0, 1] = 1; newSample[0, 2] = 0;
var prediction = result.Predict(newSample);
// Result is available in the returned value
Constructors
| Constructor | Summary |
|---|---|
MultinomialNaiveBayes(NaiveBayesOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) |
Initializes a new instance of the MultinomialNaiveBayes class. |
Methods
| Method | Summary |
|---|---|
ModelMetadata<T> GetModelMetadata() |