Online Naive Bayes

Machine Learning
Introduced by
Adapted from traditional Naive Bayes; incremental form established by the data-stream mining community (Domingos, Hulten, and others, circa 2000)
Year
2000s
Type
Probabilistic classifier (online/incremental)
Family
Machine learning

What does it do?

Online Naive Bayes is an incremental form of the Naive Bayes classifier.

It updates its class priors and per-feature sufficient statistics one labeled example (or mini-batch) at a time, so it can learn from data streams or from datasets too large to hold in memory. Because it never revisits past examples, it weights all historical evidence equally unless a forgetting mechanism is added.

The intuition behind the method

Classic Naive Bayes reads all training data at once, computes class priors and feature likelihoods, and then freezes its parameters. Online Naive Bayes instead maintains running counts or sufficient statistics (means, variances, feature frequencies) that are updated incrementally with each new example. Because the Naive Bayes conditional independence assumption means the likelihood factorises into per-feature terms, each feature's statistics can be updated independently and cheaply, making the model naturally suited to one-pass learning over streams without sacrificing the probabilistic prediction mechanism.

History

Naive Bayes is a long-standing probabilistic classifier. The incremental variant, which updates class counts and per-feature statistics as examples arrive, is available as partial_fit in some toolkits.

When to use it

Choose Online Naive Bayes when data arrive as a stream or are too large to load at once, when fast, low-memory updates matter more than model expressiveness, or when new labeled examples must be absorbed without retraining from scratch. It is a common baseline for high-dimensional sparse text (bag-of-words or TF-IDF counts) in streaming settings, and the Multinomial or Bernoulli variant fits such features directly. Do not choose it when features are strongly correlated and their interactions drive the class boundary, when a batch model can be trained on the full data and is expected to be more accurate, or when the stream is non-stationary and no forgetting strategy can be applied. Class imbalance also needs explicit handling, since the priors are learned directly from the observed label frequencies.

Limitations and considerations

  • The conditional independence assumption, under which features are independent given the class, is rarely true exactly. When correlated features carry the signal, the product of likelihoods double-counts evidence and the scores can be badly distorted.
  • Because each feature contributes its own likelihood term, the model cannot represent interactions between features, which a linear or tree-based model could capture.
  • The default update rule accumulates all past evidence with equal weight. Under concept drift the model adapts slowly, and forgetting (a sliding window, exponential decay, or resetting statistics) has to be added explicitly.
  • Gaussian Naive Bayes models each class-feature distribution as a single Gaussian. Multimodal or heavily skewed feature distributions are poorly represented by this assumption.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Each update costs work that grows with the number of features in the example and does not depend on how many examples have already been seen, so the cost per example stays flat as the stream grows.
  • Only the sufficient statistics (class counts and per-feature counts or moments) are stored, so memory does not grow with the length of the stream.
  • Outputs a score for each class derived from a probabilistic model, which can be ranked or thresholded; these scores are not automatically calibrated probabilities (see pitfalls).
  • The per-feature factorisation lets the model handle very high-dimensional inputs such as text, since each feature's statistics are maintained separately.

Applications

  • Real-time spam and phishing detection on email or message streams.
  • Incremental text classification over live news feeds or social media.
  • Sensor event classification in IoT systems where data arrives continuously.
  • Baseline classifier for high-dimensional bag-of-words or TF-IDF feature spaces.
  • Online sentiment analysis on streaming customer reviews or social posts.
  • Any classification task too large to fit in memory where a fast probabilistic model is acceptable.

How it works

How it works

1

Class priors P(C_k) = N_k / N are initialised from whatever labeled data is available at startup, or set to uniform if none is available. The per-feature sufficient statistics (counts for categorical or count features, running mean and variance for Gaussian features) are initialised in the same way.

Formula
P(C_k) = N_k / N
2

When a new labeled example (x, y) arrives, the class count N_y is incremented and the sufficient statistics of each feature x_i under class y are updated. Counts are incremented for Bernoulli or Multinomial features. For Gaussian features, the running mean and variance can be updated with Welford's online algorithm, so no earlier examples need to be stored or revisited.

3

At prediction time, the per-feature likelihoods P(x_i | C_k) are computed from the current sufficient statistics. For Gaussian Naive Bayes the Gaussian density is evaluated using the running mean and variance. For Multinomial or Bernoulli variants, smoothed frequency ratios are used.

Formula
P(x_i | C_k)
4

The score for each class is the class prior multiplied by the product of the per-feature likelihoods, and the class with the highest score is returned. In practice the computation is carried out in log space, so the product becomes a sum and numerical underflow is avoided. The scores are not normalised probabilities unless they are divided by their sum over classes.

Formula
\hat{y} = \arg\max_k P(C_k) \prod_i P(x_i | C_k)

Common pitfalls

  • Omitting Laplace (add-one) or other smoothing in the Multinomial or Bernoulli variants. A feature value unseen in a class then gives a zero likelihood that eliminates that class from the posterior.
  • Ignoring concept drift. Without a windowing or decay mechanism the statistics drift away from the current data, and accuracy on recent examples falls over a long stream.
  • Using Gaussian Naive Bayes on count data such as word counts, where the Gaussian assumption does not fit; the Multinomial variant matches count features.
  • Treating the output scores as calibrated probabilities. The independence assumption tends to push the posteriors towards extreme values, so calibration (for example with isotonic regression or Platt scaling) should be checked before the probabilities are used as such.
  • Reporting only overall accuracy under class imbalance. Per-class precision, recall and F1 show whether the minority classes are being recognised.
  • Starting the stream without the full label set. All class labels should be fixed before streaming begins, because the class-count arrays must cover every class.

Frequently asked questions

How does Online Naive Bayes handle concept drift?

By default it weights all past examples equally, so it adapts slowly when the data distribution changes. To respond faster, you can count only recent examples with a sliding window, down-weight older observations with exponential forgetting, or reset the sufficient statistics when drift is detected. The window length or decay factor is a tuning choice: set it by prequential evaluation on the stream rather than by a fixed rule.

Which variant should I use: Gaussian, Multinomial, or Bernoulli?

Use Multinomial or Bernoulli Naive Bayes for text, where features are counts or binary indicators, and Gaussian Naive Bayes for continuous features that are roughly normal within each class. The update logic differs slightly between variants, but all of them can be updated incrementally.

Is Online Naive Bayes competitive with more complex online models?

On high-dimensional sparse text it can be a reasonable choice because its update cost is low. Whether it matches online logistic regression or other online models depends on the data and should be checked on the stream at hand. On dense or strongly correlated features, a model that can represent feature interactions is more likely to be more accurate.

How should I evaluate an online classifier?

Prequential (test-then-train) evaluation predicts each example before the model is updated with it. This gives a running estimate of performance on the stream without a separate hold-out set.

Can I initialise Online Naive Bayes from a batch-trained model?

Yes. Train a batch Naive Bayes model on an initial dataset to obtain starting sufficient statistics, then switch to incremental updates. This reduces the poor early accuracy that comes from starting with uniform priors and empty statistics.

Related methods

Sources

  1. Domingos, P. & Hulten, G. (2000). Mining high-speed data streams. Proceedings of the 6th ACM SIGKDD International Conference on Knowledge Discovery and Data Mining, 71–80. ACM.

    Open DOI reference
  2. Online machine learning. Wikipedia.

    Open publication reference