OnlineNaiveBayesClassifier<T>

Models & Types · AiDotNet.Classification.Online

Implements Online (Incremental) Naive Bayes classifier.

Inheritance: ClassifierBase<T> → OnlineNaiveBayesClassifier<T>

Implements: IOnlineClassifier<T>

For Beginners

Online Naive Bayes is an incremental version of the classic Naive Bayes classifier that can learn from streaming data. It maintains running statistics (mean, variance, counts) that are updated with each new sample.

How It Works

How it works:

  • For each sample, update per-class feature statistics (mean, variance, count)
  • To predict, calculate P(class|features) using Bayes’ rule
  • Assumes features are conditionally independent given the class
  • Uses Gaussian distribution assumption for continuous features

Key formulas:

  • P(class|x) ∝ P(class) × ∏ P(xi|class)
  • P(xi|class) = N(xi; μ, σ²) for Gaussian assumption
  • Online mean update: μ_new = μ_old + (x - μ_old) / n
  • Online variance update: Welford’s algorithm

Advantages:

  • Very fast updates (constant time per sample)
  • Low memory usage (only store statistics, not data)
  • Works well with high-dimensional data
  • Naturally handles class imbalance through prior updates

Example

// Create online Naive Bayes for streaming data classification
var options = new OnlineNaiveBayesOptions<double>();
            
// Prepare training data
var features = new Matrix<double>(new double[,] { { 1.0, 1.1 }, { 1.2, 0.9 }, { 0.8, 1.0 }, { 5.0, 5.1 }, { 5.2, 4.9 }, { 4.8, 5.0 } });
var labels = new Vector<double>(new double[] { 0, 0, 0, 1, 1, 1 });
            
// Train by updating running statistics (mean, variance) incrementally
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new OnlineNaiveBayesClassifier<double>(options))
    .Build(features, labels);
            
// Predict using Bayes' rule with Gaussian likelihood
var newSample = new Matrix<double>(new double[,] { { 1.1, 1.0 } });
var prediction = result.Predict(newSample);
// Result is available in the returned value

Constructors

Constructor Summary
OnlineNaiveBayesClassifier(OnlineNaiveBayesOptions<T> options) Creates a new Online Naive Bayes classifier.

Properties

Property Summary
bool IsWarm { get; } Gets whether the model has seen at least one sample.
long SamplesSeen { get; set; } Gets the total number of samples the model has seen.

Methods

Method Summary
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters)
ModelOptions GetOptions()
Vector<T> Predict(Matrix<T> input)
Vector<T> PredictProbabilities(Vector<T> features) Gets the probability distribution over classes for a sample.
void PartialFit(Matrix<T> features, Vector<T> labels) Updates the model with a batch of training samples.
void PartialFit(Vector<T> features, T label) Updates the model with a batch of training samples.
void Train(Matrix<T> x, Vector<T> y)