mojolearn.BernoulliNB#

class mojolearn.BernoulliNB(*, alpha=1.0, force_alpha=True, binarize=0.0, fit_prior=True, class_prior=None)#

Naive Bayes classifier for binary features.

Follows scikit-learn’s BernoulliNB. X is binarized at binarize (values above the threshold become 1), then fit counts how often each feature is 1 in each class, smooths the counts by alpha and scores each class with the Bernoulli likelihood, including the features that are 0. 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.

  • binarize (float or None, default 0.0) – Threshold for binarizing X; None means X is already binary.

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

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)) – Number of rows per class in which each feature is 1.

  • feature_log_prob_ (array of shape (n_classes, n_features)) – Smoothed log probability that each feature is 1 given the class.

  • 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, 2, (1000, 20)).astype(np.float32)
>>> y = rng.integers(0, 2, 1000)
>>> model = mojolearn.BernoulliNB().fit(X, y)

See also

User guide: Preprocessing

Choosing an estimator

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.

fit_transform(X, y=None, **fit_params)#

Fit to X, then transform X.

Returns:

array – The transformed data.

get_params(deep=True)#

Return the constructor parameters as a dict.

Parameters:

deep (bool, default True) – Accepted for scikit-learn compatibility.

Returns:

dict – Parameter names mapped to their values.

partial_fit(X, y, classes=None, sample_weight=None)#

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

predict(X)#

Predict the class of each row.

Parameters:

X (array-like of shape (n_samples, n_features)) – Rows to classify.

Returns:

array of shape (n_samples,) – Predicted class labels.

predict_joint_log_proba(X)#

Return the unnormalized log score of each row and class.

Parameters:

X (array-like of shape (n_samples, n_features)) – Rows to score.

Returns:

array of shape (n_samples, n_classes) – Log scores that predict_log_proba normalizes, in the order of classes_.

predict_log_proba(X)#

Return the log of the class probabilities for each row.

Parameters:

X (array-like of shape (n_samples, n_features)) – Rows to score.

Returns:

array of shape (n_samples, n_classes) – Log probability of each class, in the order of classes_.

predict_proba(X)#

Return class probabilities for each row.

Probabilities are float32 in identical mode. In fast mode (Apple), they are float64, so that a very confident prediction does not round to exactly 1.0.

Parameters:

X (array-like of shape (n_samples, n_features)) – Rows to score.

Returns:

array of shape (n_samples, n_classes) – Probability of each class, in the order of classes_.

score(X, y)#

Return the mean accuracy on the given rows and labels.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Rows to classify.

  • y (array-like of shape (n_samples,)) – True labels.

Returns:

float – Fraction of rows classified correctly.

set_params(**params)#

Set constructor parameters.

An unknown parameter name raises ValueError.

Returns:

self