Skip to Content
OchreML
Clustering, Neighbors & NB

BernoulliNB

Multivariate Bernoulli Naive Bayes classifier for binary or threshold-binarized features with partial_fit support.

Overview & Backend Support

BernoulliNB models binary feature presence/absence using multivariate Bernoulli distributions. Unlike MultinomialNB, it explicitly penalizes the absence of features that are indicative of a class. Continuous inputs can be automatically thresholded using binarize.

  • 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 for streaming updates.
  • Persistence: Supports binary .dit serialization via save(path), BernoulliNB.load(path, device=...), ochreml.save(), and ochreml.load().

Import & Signature

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

BernoulliNB(
    alpha: float = 1.0,
    binarize: float | None = 0.0,
    fit_prior: bool = True,
    class_prior: Any | None = None,
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
alphafloat1.0Additive (Laplace/Lidstone) smoothing parameter (0.0 for no smoothing).
binarizefloat | None0.0Threshold for mapping features to {0, 1} (x > binarize). If None, inputs are assumed to already be binary.
fit_priorboolTrueWhether to learn class prior probabilities from data; if False, a uniform prior is used.
class_priorarray-like of shape (n_classes,) | NoneNoneExplicit prior probabilities of the classes. If specified, priors are not adjusted according to data.
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_log_prior_ndarray of shape (n_classes,) | NoneLog probability of each class.
feature_count_ndarray of shape (n_classes, n_features) | NoneNumber (or weighted count) of samples with active feature per class.
feature_log_prob_ndarray of shape (n_classes, n_features) | NoneEmpirical log probability of features given a class, `log P(x_i = 1
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) -> BernoulliNBFit Bernoulli Naive Bayes on (X, y).
partial_fitpartial_fit(X, y, classes=None, sample_weight=None) -> BernoulliNBIncrementally update Bernoulli feature and class 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.
loadBernoulliNB.load(path: str, device: str | None = None) -> BernoulliNBLoad a fitted model from a binary .dit file.
toto(device: str) -> BernoulliNBMove estimator compute target to another hardware device.

Example

import numpy as np
from ochreml.naive_bayes import BernoulliNB

X = np.array([
    [1, 1, 0, 0],
    [1, 0, 0, 0],
    [0, 0, 1, 1],
    [0, 0, 1, 0],
], dtype=np.float64)
y = np.array([0, 0, 1, 1], dtype=np.float64)

clf = BernoulliNB(alpha=1.0, binarize=0.0)
clf.fit(X, y)

X_test = np.array([[1, 1, 0, 0], [0, 0, 0, 1]], dtype=np.float64)
print("Predictions:", clf.predict(X_test))
print("Accuracy on train:", clf.score(X, y))

On this page