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
- x —
dict
joint_log_likelihood_many
Compute the joint log-likelihoods for a batch of feature vectors.
Parameters
- X —
pd.DataFrame
Returns
pd.DataFrame: Input samples joint log-likelihoods.
learn_many
Learn from a batch of feature vectors.
Parameters
- X —
pd.DataFrame - y —
pd.Series
learn_one
Update the model with a set of features x and a label y.
Parameters
- x —
dict[base.typing.FeatureName, Any] - y —
base.typing.ClfTarget
p_class
predict_many
Predict the outcome for each given sample.
Parameters
- X —
pd.DataFrame
Returns
pd.Series: The predicted labels.
predict_one
Predict the label of a set of features x.
Parameters
- x —
dict[base.typing.FeatureName, Any] - kwargs —
Any
Returns
base.typing.ClfTarget | None: The predicted label.
predict_proba_many
Return probabilities using the log-likelihoods in mini-batchs setting.
Parameters
- X —
pd.DataFrame
predict_proba_one
Return probabilities using the log-likelihoods.
Parameters
- x —
dict[base.typing.FeatureName, Any]