Skip to Content
OchreML
Clustering, Neighbors & NB

MultinomialNB

Multinomial Naive Bayes classifier for non-negative count and TF-IDF features with Laplace/Lidstone smoothing and partial_fit.

Overview & Backend Support

MultinomialNB implements Naive Bayes for multinomially distributed, non-negative features such as word counts or TF-IDF vectors. It supports additive Laplace/Lidstone smoothing (alpha), learned or uniform/custom class priors, sample weights, and online 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 stream large count matrices in chunks.
  • Persistence: Supports binary .dit serialization via save(path), MultinomialNB.load(path, device=...), ochreml.save(), and ochreml.load().

Import & Signature

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

MultinomialNB(
    alpha: float = 1.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).
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,) | NoneSmoothed empirical log probability for each class.
feature_count_ndarray of shape (n_classes, n_features) | NoneAccumulated feature counts (or weighted counts) per class.
feature_log_prob_ndarray of shape (n_classes, n_features) | NoneEmpirical log probability of features given a class, `log P(x_i
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) -> MultinomialNBFit Multinomial Naive Bayes on non-negative features X and targets y.
partial_fitpartial_fit(X, y, classes=None, sample_weight=None) -> MultinomialNBIncrementally update feature and class counts on a batch of non-negative 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.
loadMultinomialNB.load(path: str, device: str | None = None) -> MultinomialNBLoad a fitted model from a binary .dit file.
toto(device: str) -> MultinomialNBMove estimator compute target to another hardware device.

Example

import numpy as np
from ochreml.naive_bayes import MultinomialNB

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

clf = MultinomialNB(alpha=1.0)
clf.fit(X, y)

X_test = np.array([[2, 0, 1, 0], [0, 2, 0, 2]], dtype=np.float64)
print("Predictions:", clf.predict(X_test))
print("Probabilities:\n", clf.predict_proba(X_test))

On this page