CategoricalNaiveBayes<T>

Models & Types · AiDotNet.Classification.NaiveBayes

Categorical Naive Bayes classifier for categorical/discrete features.

Inheritance: ClassifierBase<T> → ProbabilisticClassifierBase<T> → NaiveBayesBase<T> → CategoricalNaiveBayes<T>

For Beginners

Use this when your features are categories, like:

  • Color: Red, Blue, Green
  • Size: Small, Medium, Large
  • Weather: Sunny, Rainy, Cloudy

The classifier computes: P(category_value | class) for each feature.

For example, predicting if someone will buy an umbrella:

  • P(Rainy | Will Buy) might be high
  • P(Sunny | Will Buy) might be low

Features should be encoded as integers (0, 1, 2, …) representing categories.

Use Categorical NB when:

  • Features are truly categorical (not ordinal)
  • Features can have more than 2 values (otherwise use Bernoulli)
  • Features are not counts (otherwise use Multinomial)

How It Works

Categorical Naive Bayes handles features that take on discrete categorical values. Unlike Multinomial NB (which works with counts) or Bernoulli NB (which works with binary features), Categorical NB handles multi-valued categorical features directly.

Example

var labels = new Vector<double>(new double[] { 0.0, 1.0, 0.0, 1.0 });
var newSample = new Matrix<double>(new double[,] { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 }, { 7.0, 8.0 } });
var features = new Matrix<double>(new double[,] { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 }, { 7.0, 8.0 } });
// Use AiModelBuilder facade for categoricalnaivebayes classification
var builder = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new CategoricalNaiveBayes<double>(
        new NaiveBayesOptions<double>()));
            
var result = builder.Build(features, labels);
var prediction = result.Predict(newSample);

Constructors

Constructor Summary
CategoricalNaiveBayes(NaiveBayesOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) Initializes a new instance of the CategoricalNaiveBayes class.

Methods

Method Summary
ModelMetadata<T> GetModelMetadata()