Table of Contents

Class ComplementNaiveBayes<T>

Namespace
AiDotNet.Classification.NaiveBayes
Assembly
AiDotNet.dll

Complement Naive Bayes classifier designed for imbalanced datasets.

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

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

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

Constructors

ComplementNaiveBayes(NaiveBayesOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?, bool)

Initializes a new instance of the ComplementNaiveBayes class.

public ComplementNaiveBayes(NaiveBayesOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null, bool normalize = true)

Parameters

options NaiveBayesOptions<T>

Configuration options for the classifier.

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

Optional regularization strategy.

normalize bool

Whether to normalize feature weights (default true).

Methods

Clone()

Creates a clone of the classifier model.

public override IFullModel<T, Matrix<T>, Vector<T>> Clone()

Returns

IFullModel<T, Matrix<T>, Vector<T>>

A new instance of the model with the same parameters and options.

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

Computes class-specific parameters for Complement Naive Bayes.

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

Parameters

x Matrix<T>
y Vector<T>

ComputeLogLikelihood(Vector<T>, int)

Computes the log-likelihood for a sample given a class.

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

Parameters

sample Vector<T>
classIndex int

Returns

T

CreateNewInstance()

Creates a new instance of the same type as this classifier.

protected override IFullModel<T, Matrix<T>, Vector<T>> CreateNewInstance()

Returns

IFullModel<T, Matrix<T>, Vector<T>>

A new instance of the same classifier type.

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