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:
N1reduction primitives across CPU (Rayon), CUDA,wgpu, and Google TPU. - Incremental learning (
partial_fit): Supported. Passclasseson the firstpartial_fit(X_batch, y_batch, classes=all_classes, sample_weight=...)call to stream large count matrices in chunks. - Persistence: Supports binary
.ditserialization viasave(path),MultinomialNB.load(path, device=...),ochreml.save(), andochreml.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
| Parameter | Type | Default | Description |
|---|---|---|---|
alpha | float | 1.0 | Additive (Laplace/Lidstone) smoothing parameter (0.0 for no smoothing). |
fit_prior | bool | True | Whether to learn class prior probabilities from data; if False, a uniform prior is used. |
class_prior | array-like of shape (n_classes,) | None | None | Explicit prior probabilities of the classes. If specified, priors are not adjusted according to data. |
device | str | None | None | Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
classes_ | ndarray of shape (n_classes,) | None | Unique class labels (float64) known to the classifier. |
class_count_ | ndarray of shape (n_classes,) | None | Number (or total weight) of training samples observed in each class. |
class_log_prior_ | ndarray of shape (n_classes,) | None | Smoothed empirical log probability for each class. |
feature_count_ | ndarray of shape (n_classes, n_features) | None | Accumulated feature counts (or weighted counts) per class. |
feature_log_prob_ | ndarray of shape (n_classes, n_features) | None | Empirical log probability of features given a class, `log P(x_i |
n_features_in_ | int | None | Number of features seen during fit or partial_fit. |
device_ | str | None | Resolved hardware device string used by the underlying Rust model. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y, sample_weight=None) -> MultinomialNB | Fit Multinomial Naive Bayes on non-negative features X and targets y. |
partial_fit | partial_fit(X, y, classes=None, sample_weight=None) -> MultinomialNB | Incrementally update feature and class counts on a batch of non-negative samples. |
predict | predict(X) -> ndarray | Predict class labels (float64, shape (n_samples,)) for X. |
predict_proba | predict_proba(X) -> ndarray | Return posterior class probabilities of shape (n_samples, n_classes). |
predict_log_proba | predict_log_proba(X) -> ndarray | Return log-probability estimates of shape (n_samples, n_classes). |
predict_joint_log_likelihood | predict_joint_log_likelihood(X) -> ndarray | Return unnormalized posterior log-probabilities of shape (n_samples, n_classes). |
score | score(X, y) -> float | Return mean classification accuracy on (X, y). |
save | save(path: str) -> None | Save the fitted model to a binary .dit file. |
load | MultinomialNB.load(path: str, device: str | None = None) -> MultinomialNB | Load a fitted model from a binary .dit file. |
to | to(device: str) -> MultinomialNB | Move 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))