Implementation:Online ml River NaiveBayes GaussianNB
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Naive_Bayes, Classification |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Gaussian Naive Bayes assumes features follow Gaussian distributions and is suitable for continuous-valued features in classification tasks.
Description
This implementation maintains a Gaussian distribution (mean and variance) for each feature-class combination using the online Gaussian estimator from river.proba. During training, each feature's Gaussian is updated with new values. For prediction, it computes joint log-likelihood by summing log probabilities from each feature's Gaussian PDF evaluated at the feature value. Probabilities are obtained by exponentiating and normalizing the log-likelihoods using log-sum-exp for numerical stability.
Usage
Use Gaussian NB for classification with continuous features that approximately follow normal distributions. It's fast, interpretable, and works well as a baseline. Unlike other NB variants, it doesn't require count or text features. Works directly with numerical features without special preprocessing. Best for low-dimensional problems where feature independence assumptions are reasonable.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/naive_bayes/gaussian.py
Signature
class GaussianNB(base.Classifier):
def __init__(self):
self.class_counts = collections.Counter()
self.gaussians = collections.defaultdict(
functools.partial(collections.defaultdict, proba.Gaussian)
)
Import
from river import naive_bayes
I/O Contract
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| (none) | No parameters - uses default Gaussian priors |
Attributes
| Attribute | Type | Description |
|---|---|---|
| class_counts | Counter | Number of instances per class |
| gaussians | defaultdict | Gaussian distributions per feature-class pair |
Input/Output
| Method | Input | Output |
|---|---|---|
| learn_one | x: dict, y: Any | None |
| predict_proba_one | x: dict | dict |
| predict_one | x: dict | Any |
Usage Examples
from river import naive_bayes
from river import stream
import numpy as np
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
Y = np.array([1, 1, 1, 2, 2, 2])
model = naive_bayes.GaussianNB()
for x, y in stream.iter_array(X, Y):
model.learn_one(x, y)
model.predict_one({0: -0.8, 1: -1})
# 1
# Access learned parameters
model.p_class(1)
# 0.5
model.p_class(2)
# 0.5
# Get probabilities
model.predict_proba_one({0: -0.8, 1: -1})
# {1: 0.999..., 2: 0.000...}
# Examine learned Gaussians
model.gaussians[1][0] # Gaussian for feature 0, class 1
# 𝒩(μ=-2.000, σ=0.816)