Classify indicator variables
Binary or Boolean tabular features
Scikit-Learn · Class
BernoulliNB is a Naive Bayes classifier for multivariate Bernoulli data. It is designed for binary or Boolean features and can optionally binarize numeric inputs.
BernoulliNB applies Naive Bayes to features treated as binary indicators. It can threshold non-binary inputs with binarize, estimates class priors and feature probabilities during fit, and predicts classes using both present and absent feature evidence.
class sklearn.naive_bayes.BernoulliNB(*, alpha=1.0, force_alpha=True, binarize=0.0, fit_prior=True, class_prior=None)| Name | Type | Description |
|---|---|---|
alpha | float | Additive smoothing parameter; default 1.0. |
binarize | float or None | Threshold for converting sample features to Boolean values; default 0.0. |
fit_prior | bool | Whether to learn class prior probabilities; default True. |
class_prior | array-like | Prior probabilities for the classes; default None. |
force_alpha | bool | If true, force alpha to be positive; default True. |
A BernoulliNB classifier instance; fit returns the fitted estimator, while prediction methods return class labels or probabilities.
from sklearn.naive_bayes import BernoulliNB
X = [[1, 0, 1], [0, 1, 0], [1, 1, 0], [0, 0, 1]]
y = [1, 0, 1, 0]
model = BernoulliNB()
model.fit(X, y)
print(model.predict([[1, 0, 0]]))Fits a Bernoulli Naive Bayes classifier on binary feature vectors and predicts the class of a new vector.
Binary or Boolean tabular features
Text represented by word-presence vectors
Numeric features requiring thresholding
BernoulliNB is intended for multivariate Bernoulli data, where each feature is a binary or Boolean variable. It differs from MultinomialNB, which models occurrence counts.
Follow-up: When can it be preferable for text? Word occurrence vectors can be useful, and the documentation notes possible advantages on shorter documents.
The binarize parameter supplies a threshold for converting numeric features to Boolean indicators. With binary input already prepared, use binarize=None so the values are treated as existing binary vectors.
Follow-up: Why does the distinction matter? The model's Bernoulli likelihood accounts for both feature presence and feature absence.
Its decision rule includes terms for both a feature being present and not present. Consequently, non-occurrence contributes evidence, unlike the multinomial rule that ignores a non-occurring feature.
Follow-up: What should be compared in a text task? Evaluate BernoulliNB against MultinomialNB because performance depends on the dataset, including document length.
MultinomialNBCategoricalNBComplementNBGaussianNBNaive Bayes