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:
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 register all possible classes before streaming subsequent batches. - Persistence: Supports binary
.ditserialization viasave(path),GaussianNB.load(path, device=...),ochreml.save(), andochreml.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
| Parameter | Type | Default | Description |
|---|---|---|---|
priors | array-like of shape (n_classes,) | None | None | Fixed prior probabilities of the classes. If specified, priors are not estimated from data. |
var_smoothing | float | 1e-9 | Portion of the largest variance across all features added to variances for numerical stability. |
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_prior_ | ndarray of shape (n_classes,) | None | Probability of each class. |
theta_ | ndarray of shape (n_classes, n_features) | None | Mean of each feature per class. |
var_ | ndarray of shape (n_classes, n_features) | None | Variance of each feature per class (including epsilon_ smoothing). |
epsilon_ | float | Absolute additive value added to variances (var_smoothing * max_var). |
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) -> GaussianNB | Fit Gaussian Naive Bayes on (X, y). |
partial_fit | partial_fit(X, y, classes=None, sample_weight=None) -> GaussianNB | Incrementally update class means, variances, and counts on a batch of 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 | GaussianNB.load(path: str, device: str | None = None) -> GaussianNB | Load a fitted model from a binary .dit file. |
to | to(device: str) -> GaussianNB | Move 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]])))