Clustering, Neighbors & NB
ComplementNB
Complement Naive Bayes classifier tailored for imbalanced non-negative count datasets with optional weight normalization and partial_fit.
Overview & Backend Support
ComplementNB adapts the Multinomial Naive Bayes algorithm for imbalanced datasets by estimating feature weights from the complement of each class (all samples not belonging to class c) rather than only samples within class c. Optional L1 weight normalization (norm=True) further stabilizes text classification performance.
- 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 non-negative batches incrementally. - Persistence: Supports binary
.ditserialization viasave(path),ComplementNB.load(path, device=...),ochreml.save(), andochreml.load().
Import & Signature
from ochreml.naive_bayes import ComplementNB
# or lazily from top-level:
from ochreml import ComplementNB
ComplementNB(
alpha: float = 1.0,
fit_prior: bool = True,
class_prior: Any | None = None,
norm: bool = False,
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. |
norm | bool | False | Whether to apply a second L1 normalization step to the complement feature weights. |
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_all_ | ndarray of shape (n_features,) | None | Total count of each feature across all classes. |
feature_log_prob_ | ndarray of shape (n_classes, n_features) | None | Complement log-weight matrix for each class and feature. |
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) -> ComplementNB | Fit Complement Naive Bayes on non-negative features X and targets y. |
partial_fit | partial_fit(X, y, classes=None, sample_weight=None) -> ComplementNB | Incrementally update complement counts and weights 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 | ComplementNB.load(path: str, device: str | None = None) -> ComplementNB | Load a fitted model from a binary .dit file. |
to | to(device: str) -> ComplementNB | Move estimator compute target to another hardware device. |
Example
import numpy as np
from ochreml.naive_bayes import ComplementNB
# Imbalanced count dataset (class 0 has 4 samples, class 1 has 1 sample)
X = np.array([
[4, 1, 0, 0],
[3, 2, 0, 0],
[5, 0, 1, 0],
[3, 1, 0, 0],
[0, 0, 4, 3],
], dtype=np.float64)
y = np.array([0, 0, 0, 0, 1], dtype=np.float64)
clf = ComplementNB(alpha=1.0, norm=True)
clf.fit(X, y)
X_test = np.array([[4, 1, 0, 0], [0, 0, 3, 2]], dtype=np.float64)
print("Predictions:", clf.predict(X_test))
print("Probabilities:\n", clf.predict_proba(X_test))