Class CategoricalNaiveBayes<T>
- Namespace
- AiDotNet.Classification.NaiveBayes
- Assembly
- AiDotNet.dll
Categorical Naive Bayes classifier for categorical/discrete features.
public class CategoricalNaiveBayes<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
TThe numeric data type used for calculations (e.g., float, double).
- Inheritance
-
CategoricalNaiveBayes<T>
- Implements
-
IClassifier<T>
- Inherited Members
- Extension Methods
Remarks
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.
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)
Constructors
CategoricalNaiveBayes(NaiveBayesOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?)
Initializes a new instance of the CategoricalNaiveBayes class.
public CategoricalNaiveBayes(NaiveBayesOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)
Parameters
optionsNaiveBayesOptions<T>Configuration options for the classifier.
regularizationIRegularization<T, Matrix<T>, Vector<T>>Optional regularization strategy.
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 category probabilities for all classes.
protected override void ComputeClassParameters(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>yVector<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
sampleVector<T>classIndexint
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()