mojolearn.CategoricalNB#
- class mojolearn.CategoricalNB(*, alpha=1.0, force_alpha=True, fit_prior=True, class_prior=None, min_categories=None)[source]#
Bases:
_DiscreteNBNaive Bayes classifier for categorical features.
Follows scikit-learn’s
CategoricalNB. Each column ofXholds category indices0, 1, .... Fit counts each category per class and feature, smooths the counts byalphaand stores their log share of the class’s rows. Negative values raiseValueErrorin fit; at prediction, a category index outside the fitted range raisesIndexError, as in scikit-learn. Computation is float32.fitacceptssample_weight, which weights the counts.partial_fit(X, y, classes=None, sample_weight=None)adds each batch’s category counts to the running ones, widening them as new categories appear;classesis required on the first call. Afterpartial_fit,n_categories_is the width of the accumulated counts (scikit-learn narrows it to the last batch’s).- Parameters:
alpha (float, default 1.0) – Additive smoothing. Must be greater than 0;
alpha <= 0raisesNotImplementedError.force_alpha (bool, default True) – Accepted for scikit-learn compatibility.
fit_prior (bool, default True) – Learn class priors from the data; when False, use a uniform prior.
class_prior (array-like of shape (n_classes,), default None) – Given class priors (nonnegative); overrides
fit_prior.min_categories (int or array-like of shape (n_features,), default None) – Minimum number of categories per feature; the count of categories is the larger of this and the largest index seen plus one.
- Variables:
classes_ (array of shape (n_classes,)) – Class labels.
class_count_ (array of shape (n_classes,)) – Number (or weighted number) of training rows per class.
class_log_prior_ (array of shape (n_classes,)) – Log prior of each class.
category_count_ (list of arrays) – Per feature, an array of shape (n_classes, n_categories) of category counts.
feature_log_prob_ (list of arrays) – Per feature, an array of shape (n_classes, n_categories) of smoothed log probabilities.
n_categories_ (array of int64) – Number of categories of each feature.
n_features_in_ (int) – Number of columns seen in fit.
numeric_mode_ (str) – Numeric mode the estimator was fitted in; prediction runs in the same mode.
Examples
>>> import numpy as np, mojolearn >>> rng = np.random.default_rng(0) >>> X = rng.integers(0, 4, (1000, 6)).astype(np.float32) >>> y = rng.integers(0, 2, 1000) >>> model = mojolearn.CategoricalNB().fit(X, y)
- __init__(*, alpha=1.0, force_alpha=True, fit_prior=True, class_prior=None, min_categories=None)[source]#
Methods
fit(X, y[, sample_weight])fit_transform(X[, y])Fit to
X, then transformX.get_params([deep])Return the constructor parameters as a dict.
partial_fit(X, y[, classes, sample_weight])Update the model with one batch of rows.
predict(X)Predict the class of each row.
predict_joint_log_proba(X)Return the unnormalized log score of each row and class.
predict_log_proba(X)Return the log of the class probabilities for each row.
predict_proba(X)Return class probabilities for each row.
score(X, y)Return the mean accuracy on the given rows and labels.
set_params(**params)Set constructor parameters.
- partial_fit(X, y, classes=None, sample_weight=None)[source]#
Update the model with one batch of rows.
The batch’s class and feature counts are added to the running ones, then the log probabilities and the class log prior are recomputed from the sums, as in scikit-learn.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Training rows.
y (array-like of shape (n_samples,)) – Class labels.
classes (array-like, default None) – Every class label. Required on the first call; later calls may pass it only unchanged.
sample_weight (array-like of shape (n_samples,), default None) – Weights of the rows.
- Returns:
self