ComplementNB¶
Naive Bayes classifier for multinomial models.
Complement Naive Bayes model learns from occurrences between features such as word counts and discrete classes. ComplementNB is suitable for imbalance dataset. The input vector must contain positive values, such as counts or TF-IDF values.
Parameters¶
-
alpha
Default →
1.0
Additive (Laplace/Lidstone) smoothing parameter (use 0 for no smoothing).
Attributes¶
-
class_dist (proba.Multinomial)
Class prior probability distribution.
-
feature_counts (collections.defaultdict)
Total frequencies per feature and class.
-
class_totals (collections.Counter)
Total frequencies per class.
Examples¶
import pandas as pd
from river import compose
from river import feature_extraction
from river import naive_bayes
docs = [
("Chinese Beijing Chinese", "yes"),
("Chinese Chinese Shanghai", "yes"),
("Chinese Macao", "maybe"),
("Tokyo Japan Chinese", "no")
]
model = compose.Pipeline(
("tokenize", feature_extraction.BagOfWords(lowercase=False)),
("nb", naive_bayes.ComplementNB(alpha=1))
)
for sentence, label in docs:
model.learn_one(sentence, label)
model["nb"].p_class("yes")
0.5
model["nb"].p_class("no")
0.25
model["nb"].p_class("maybe")
0.25
model.predict_proba_one("test")
{'yes': 0.275, 'maybe': 0.375, 'no': 0.35}
model.predict_one("test")
'maybe'
You can train the model and make predictions in mini-batch mode using the class methods
learn_many
and predict_many
.
df_docs = pd.DataFrame(docs, columns = ["docs", "y"])
X = pd.Series([
"Chinese Beijing Chinese",
"Chinese Chinese Shanghai",
"Chinese Macao",
"Tokyo Japan Chinese"
])
y = pd.Series(["yes", "yes", "maybe", "no"])
model = compose.Pipeline(
("tokenize", feature_extraction.BagOfWords(lowercase=False)),
("nb", naive_bayes.ComplementNB(alpha=1))
)
model.learn_many(X, y)
unseen = pd.Series(["Taiwanese Taipei", "Chinese Shanghai"])
model.predict_proba_many(unseen)
maybe no yes
0 0.415129 0.361624 0.223247
1 0.248619 0.216575 0.534807
model.predict_many(unseen)
0 maybe
1 yes
dtype: object
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 — 'pd.DataFrame'
Returns
pd.DataFrame: Input samples joint log likelihood.
learn_many
Learn from a batch of count vectors.
Parameters
- X — 'pd.DataFrame'
- y — 'pd.Series'
learn_one
Updates the model with a single observation.
Parameters
- x — 'dict'
- y — 'base.typing.ClfTarget'
p_class
p_class_many
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'
- kwargs
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'