Skip to content

GaussianNB

Gaussian Naive Bayes.

A Gaussian distribution \(G_{cf}\) is maintained for each class \(c\) and each feature \(f\). Each Gaussian is updated using the amount associated with each feature; the details can be be found in proba.Gaussian. The joint log-likelihood is then obtained by summing the log probabilities of each feature associated with each class.

Examples

from river import naive_bayes
from river import stream
import numpy as np

X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
Y = np.array([1, 1, 1, 2, 2, 2])

model = naive_bayes.GaussianNB()

for x, y in stream.iter_array(X, Y):
    model.learn_one(x, y)

model.predict_one({0: -0.8, 1: -1})
1

You can also train the model and make predictions in mini-batch mode.

import pandas as pd

model = naive_bayes.GaussianNB()
model.learn_many(pd.DataFrame(X), pd.Series(Y))
model.predict_many(pd.DataFrame([[-0.8, -1], [2.8, 1.5]]))
0    1
1    2
dtype: int64

Methods

joint_log_likelihood

Compute the unnormalized posterior log-likelihood of x.

The log-likelihood is log P(c) + log P(x|c).

Parameters

  • xdict

joint_log_likelihood_many

Compute the joint log-likelihoods for a batch of feature vectors.

Parameters

  • Xpd.DataFrame

Returns

pd.DataFrame: Input samples joint log-likelihoods.

learn_many

Learn from a batch of feature vectors.

Parameters

  • Xpd.DataFrame
  • ypd.Series

learn_one

Update the model with a set of features x and a label y.

Parameters

  • xdict[base.typing.FeatureName, Any]
  • ybase.typing.ClfTarget

p_class
predict_many

Predict the outcome for each given sample.

Parameters

  • Xpd.DataFrame

Returns

pd.Series: The predicted labels.

predict_one

Predict the label of a set of features x.

Parameters

  • xdict[base.typing.FeatureName, Any]
  • kwargsAny

Returns

base.typing.ClfTarget | None: The predicted label.

predict_proba_many

Return probabilities using the log-likelihoods in mini-batchs setting.

Parameters

  • Xpd.DataFrame

predict_proba_one

Return probabilities using the log-likelihoods.

Parameters

  • xdict[base.typing.FeatureName, Any]