ComplementNaiveBayes<T>

Models & Types · AiDotNet.Classification.NaiveBayes

Complement Naive Bayes classifier designed for imbalanced datasets.

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

For Beginners

Think of it this way: instead of asking “how likely is this word in spam?”, CNB asks “how unlikely is this word in NOT-spam?”

This helps when:

  • One class has many more examples than others
  • Features are not uniformly distributed across classes
  • Standard Multinomial NB is biased toward the majority class

Example: In text classification with 95% non-spam and 5% spam, standard NB might always predict non-spam. CNB corrects this.

CNB is particularly effective for:

  • Text classification with imbalanced classes
  • Sentiment analysis
  • Topic categorization

How It Works

Complement Naive Bayes (CNB) addresses some of the drawbacks of the standard Multinomial Naive Bayes, particularly on imbalanced datasets. Instead of computing P(feature|class), it computes P(feature|NOT class).

Example

// Create Complement NB for imbalanced text classification
var options = new NaiveBayesOptions<double>();
            
// Prepare word count features (term-frequency vectors)
var features = new Matrix<double>(4, 3);
features[0, 0] = 3; features[0, 1] = 0; features[0, 2] = 1; // Class 0
features[1, 0] = 2; features[1, 1] = 0; features[1, 2] = 2; // Class 0
features[2, 0] = 0; features[2, 1] = 3; features[2, 2] = 1; // Class 1
features[3, 0] = 0; features[3, 1] = 2; features[3, 2] = 2; // Class 1
var labels = new Vector<double>(new double[] { 0, 0, 1, 1 });
            
// Train using complement class statistics to handle imbalance
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new ComplementNaiveBayes<double>(options))
    .Build(features, labels);
            
// Predict class using complement likelihood ratio
var newSample = new Matrix<double>(1, 3);
newSample[0, 0] = 3; newSample[0, 1] = 0; newSample[0, 2] = 1;
var prediction = result.Predict(newSample);
// Result is available in the returned value

Constructors

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

Methods

Method Summary
ModelMetadata<T> GetModelMetadata()