mojolearn.GaussianNB#

class mojolearn.GaussianNB(*, priors=None, var_smoothing=1e-09)[source]#

Bases: _Classifier

Gaussian naive Bayes classifier.

Follows scikit-learn’s GaussianNB. Fit learns each class’s per-feature mean and population variance, adds var_smoothing times the largest feature variance to every variance, and predicts with the Gaussian likelihood. Computation is float32.

fit accepts sample_weight, which weights the class means, variances and counts as in scikit-learn. partial_fit(X, y, classes=None, sample_weight=None) merges each batch into the running counts, means and variances; classes is required on the first call (ValueError otherwise) and may only be repeated unchanged afterwards.

Parameters:
  • priors (array-like of shape (n_classes,), default None) – Class prior probabilities; nonnegative and summing to one (ValueError otherwise). None uses the class frequencies in the training data.

  • var_smoothing (float, default 1e-9) – Fraction of the largest feature variance added to every variance.

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_prior_ (array of shape (n_classes,)) – Probability of each class.

  • theta_ (array of shape (n_classes, n_features)) – Mean of each feature per class.

  • var_ (array of shape (n_classes, n_features)) – Smoothed variance of each feature per class.

  • epsilon_ (float) – Value added to the variances.

  • 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.random((1000, 8), dtype=np.float32)
>>> y = (X[:, 0] > 0.5).astype(np.int64)
>>> model = mojolearn.GaussianNB().fit(X, y)
>>> model.predict(X[:5])
__init__(*, priors=None, var_smoothing=1e-09)[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])

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.