Table of Contents

Class GaussianNaiveBayes<T>

Namespace
AiDotNet.Classification.NaiveBayes
Assembly
AiDotNet.dll

Gaussian Naive Bayes classifier for continuous features.

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

Gaussian Naive Bayes assumes that the continuous features follow a Gaussian (normal) distribution within each class. It estimates the mean and variance of each feature for each class during training, then uses these to compute the probability density during prediction.

For Beginners: This classifier works well with continuous data (like measurements: height, weight, temperature). It assumes each feature follows a bell-shaped curve (normal distribution) for each class.

During training, it learns:

  • The average value of each feature for each class
  • How spread out (variance) each feature is for each class

During prediction, it calculates how likely a new data point is under each class's distribution and picks the most likely class.

Example use cases:

  • Classifying iris flowers based on petal/sepal measurements
  • Medical diagnosis based on patient vitals
  • Weather prediction based on sensor readings

Constructors

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

Initializes a new instance of the GaussianNaiveBayes class.

public GaussianNaiveBayes(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 GaussianNaiveBayes instance.

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

Computes the mean and variance of each feature for each class.

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

Parameters

x Matrix<T>

The input features matrix.

y Vector<T>

The target class labels vector.

ComputeLogLikelihood(Vector<T>, int)

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

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

Parameters

sample Vector<T>

The feature vector for a single sample.

classIndex int

The class index.

Returns

T

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

Remarks

The Gaussian log-likelihood for a feature x given class c is: log P(x|c) = -0.5 * [log(2*pi) + log(variance) + (x - mean)^2 / variance]

The total log-likelihood is the sum over all features (assuming independence).

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

Deserialize(byte[])

Deserializes the model from a byte array.

public override void Deserialize(byte[] modelData)

Parameters

modelData byte[]

The byte array containing the serialized model data.

Remarks

This method reconstructs the model's parameters from a serialized byte array.

For Beginners: Deserialization is the opposite of serialization - it takes the saved model data and reconstructs the model's internal state. This allows you to load a previously trained model and use it to make predictions without having to retrain it.

Exceptions

InvalidOperationException

Thrown when deserialization fails.

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

Serialize()

Serializes the model to a byte array.

public override byte[] Serialize()

Returns

byte[]

A byte array containing the serialized model data.

Remarks

This method serializes the model's parameters to a JSON format and then converts it to a byte array.

For Beginners: Serialization converts the model's internal state into a format that can be saved to disk or transmitted over a network. This allows you to save a trained model and load it later without having to retrain it.