CategoricalNB¶
Naive Bayes classifier for categorical features.
Categorical Naive Bayes learns a separate model for each categorical feature. For each class, a frequency is maintained for every value seen for every feature. Prediction is done by computing the joint log-likelihood of each class given the observed category values.
Parameters¶
-
alpha
Default →
1.0Additive (Laplace/Lidstone) smoothing parameter (use 0 for no smoothing).
Attributes¶
-
class_counts (
collections.Counter)Number of times each class has been seen.
-
feature_counts (
collections.defaultdict)Total frequencies per feature, value, and class.
Examples¶
from river import naive_bayes
X = [
{"outlook": "sunny", "temp": "hot"},
{"outlook": "sunny", "temp": "hot"},
{"outlook": "sunny", "temp": "mild"},
{"outlook": "sunny", "temp": "cool"},
{"outlook": "rainy", "temp": "cool"},
{"outlook": "rainy", "temp": "cool"},
{"outlook": "rainy", "temp": "mild"},
{"outlook": "rainy", "temp": "hot"},
{"outlook": "overcast", "temp": "cool"},
{"outlook": "overcast", "temp": "mild"},
{"outlook": "overcast", "temp": "mild"},
{"outlook": "overcast", "temp": "hot"},
]
y = ["no", "no", "no", "no", "yes", "yes", "yes", "yes",
"yes", "yes", "yes", "yes"]
model = naive_bayes.CategoricalNB(alpha=1)
for x, yi in zip(X, y):
model.learn_one(x, yi)
model.predict_one({"outlook": "sunny", "temp": "mild"})
'no'
model.predict_proba_one({"outlook": "sunny", "temp": "mild"})
{'no': 0.755..., 'yes': 0.244...}
You can also train and predict in mini-batch mode.
import pandas as pd
df = pd.DataFrame(X)
y = pd.Series(y)
batch_model = naive_bayes.CategoricalNB(alpha=1)
batch_model.learn_many(df, y)
unseen = pd.DataFrame([{"outlook": "rainy", "temp": "cool"}])
batch_model.predict_many(unseen)
0 yes
dtype: object
batch_model.predict_proba_many(unseen)
no yes
0 0.109900 0.890100
Methods¶
joint_log_likelihood
Computes the joint log likelihood of input features.
Parameters
- x —
dict
Returns
float: Mapping between classes and joint log likelihood.
joint_log_likelihood_many
Computes the joint log likelihood of input features.
Parameters
- X —
IntoDataFrame
Returns
IntoDataFrame: Input samples joint log likelihood.
learn_many
Learn from a batch of categorical feature vectors.
Parameters
- X —
IntoDataFrame - y —
IntoSeries
learn_one
Updates the model with a single observation.
Parameters
- x —
dict[base.typing.FeatureName, Any] - y —
base.typing.ClfTarget
p_class
p_class_many
p_feature_given_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 —
IntoDataFrame
predict_proba_one
Return probabilities using the log-likelihoods.
Parameters
- x —
dict[base.typing.FeatureName, Any]