Skip to Content
OchreML
Clustering, Neighbors & NB

GaussianNB

Gaussian Naive Bayes classifier for continuous features with sample weights and online partial_fit updates.

Overview & Backend Support

GaussianNB models each feature within each class as an independent Gaussian distribution with per-class mean (theta_) and variance (var_). It supports weighted samples (sample_weight), custom class priors, variance smoothing (var_smoothing), and incremental out-of-core updates via partial_fit.

  • Backend primitives: N1 reduction primitives across CPU (Rayon), CUDA, wgpu, and Google TPU.
  • Incremental learning (partial_fit): Supported. Pass classes on the first partial_fit(X_batch, y_batch, classes=all_classes, sample_weight=...) call to register all possible classes before streaming subsequent batches.
  • Persistence: Supports binary .dit serialization via save(path), GaussianNB.load(path, device=...), ochreml.save(), and ochreml.load().

Import & Signature

from ochreml.naive_bayes import GaussianNB
# or lazily from top-level:
from ochreml import GaussianNB

GaussianNB(
    priors: Any | None = None,
    var_smoothing: float = 1e-9,
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
priorsarray-like of shape (n_classes,) | NoneNoneFixed prior probabilities of the classes. If specified, priors are not estimated from data.
var_smoothingfloat1e-9Portion of the largest variance across all features added to variances for numerical stability.
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
classes_ndarray of shape (n_classes,) | NoneUnique class labels (float64) known to the classifier.
class_count_ndarray of shape (n_classes,) | NoneNumber (or total weight) of training samples observed in each class.
class_prior_ndarray of shape (n_classes,) | NoneProbability of each class.
theta_ndarray of shape (n_classes, n_features) | NoneMean of each feature per class.
var_ndarray of shape (n_classes, n_features) | NoneVariance of each feature per class (including epsilon_ smoothing).
epsilon_floatAbsolute additive value added to variances (var_smoothing * max_var).
n_features_in_int | NoneNumber of features seen during fit or partial_fit.
device_str | NoneResolved hardware device string used by the underlying Rust model.

Methods

MethodSignatureDescription
fitfit(X, y, sample_weight=None) -> GaussianNBFit Gaussian Naive Bayes on (X, y).
partial_fitpartial_fit(X, y, classes=None, sample_weight=None) -> GaussianNBIncrementally update class means, variances, and counts on a batch of samples.
predictpredict(X) -> ndarrayPredict class labels (float64, shape (n_samples,)) for X.
predict_probapredict_proba(X) -> ndarrayReturn posterior class probabilities of shape (n_samples, n_classes).
predict_log_probapredict_log_proba(X) -> ndarrayReturn log-probability estimates of shape (n_samples, n_classes).
predict_joint_log_likelihoodpredict_joint_log_likelihood(X) -> ndarrayReturn unnormalized posterior log-probabilities of shape (n_samples, n_classes).
scorescore(X, y) -> floatReturn mean classification accuracy on (X, y).
savesave(path: str) -> NoneSave the fitted model to a binary .dit file.
loadGaussianNB.load(path: str, device: str | None = None) -> GaussianNBLoad a fitted model from a binary .dit file.
toto(device: str) -> GaussianNBMove estimator compute target to another hardware device.

Example

import numpy as np
from ochreml.naive_bayes import GaussianNB

X1 = np.array([[-1.0, -1.0], [-2.0, -1.5], [1.0, 1.0], [2.0, 1.5]], dtype=np.float64)
y1 = np.array([0, 0, 1, 1], dtype=np.float64)
X2 = np.array([[-1.2, -0.8], [1.3, 1.1]], dtype=np.float64)
y2 = np.array([0, 1], dtype=np.float64)

clf = GaussianNB(var_smoothing=1e-9)
clf.partial_fit(X1, y1, classes=[0, 1])
clf.partial_fit(X2, y2)

print("Predictions:", clf.predict(np.array([[-1.5, -1.0], [1.5, 1.2]])))
print("Probabilities:\n", clf.predict_proba(np.array([[-1.5, -1.0], [1.5, 1.2]])))

On this page