mojolearn.ComplementNB#

class mojolearn.ComplementNB(*, alpha=1.0, force_alpha=True, fit_prior=True, class_prior=None, norm=False)[source]#

Bases: _DiscreteNB

Complement naive Bayes classifier, suited to imbalanced text data.

Follows scikit-learn’s ComplementNB. Fit learns, for each class, the smoothed feature counts of all the other classes and stores their negated log share (normalized when norm=True). As in scikit-learn, the class prior is used only when there is a single class. X must be nonnegative (ValueError otherwise). Computation is float32.

fit accepts sample_weight, which weights the counts. partial_fit(X, y, classes=None, sample_weight=None) adds each batch’s counts to the running ones; classes is required on the first call.

Parameters:
  • alpha (float, default 1.0) – Additive smoothing. Must be greater than 0; alpha <= 0 raises NotImplementedError.

  • 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.

  • norm (bool, default False) – Normalize the weights of each class.

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.

  • feature_count_ (array of shape (n_classes, n_features)) – Sum of each feature per class.

  • feature_log_prob_ (array of shape (n_classes, n_features)) – Class weights computed from the complement counts.

  • 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, 5, (1000, 20)).astype(np.float32)
>>> y = rng.integers(0, 3, 1000)
>>> model = mojolearn.ComplementNB().fit(X, y)
__init__(*, alpha=1.0, force_alpha=True, fit_prior=True, class_prior=None, norm=False)[source]#

Methods

fit(X, y[, sample_weight])

fit_transform(X[, y])

Fit to X, then transform X.

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.