Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

BernoulliNB

BernoulliNB is a Naive Bayes classifier for multivariate Bernoulli data. It is designed for binary or Boolean features and can optionally binarize numeric inputs.

How it works

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.

Syntax

class sklearn.naive_bayes.BernoulliNB(*, alpha=1.0, force_alpha=True, binarize=0.0, fit_prior=True, class_prior=None)

Parameters

NameTypeDescription
alphafloatAdditive smoothing parameter; default 1.0.
binarizefloat or NoneThreshold for converting sample features to Boolean values; default 0.0.
fit_priorboolWhether to learn class prior probabilities; default True.
class_priorarray-likePrior probabilities for the classes; default None.
force_alphaboolIf true, force alpha to be positive; default True.

Returns

A BernoulliNB classifier instance; fit returns the fitted estimator, while prediction methods return class labels or probabilities.

Key points

  • Use BernoulliNB for classification when features represent binary events across multiple dimensions.
  • Its feature representation is Boolean-style rather than count-based.
  • alpha controls additive smoothing of the estimated probabilities.
  • binarize determines how numeric sample values become Boolean indicators.
  • fit_prior controls whether class proportions are learned from training data.
  • class_prior lets you provide class priors explicitly instead of deriving them from observations.

Example: Train and predict with binary features

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.

When to use it

Classify indicator variables

Binary or Boolean tabular features

Use word occurrence features

Text represented by word-presence vectors

Binarize inputs before modeling

Numeric features requiring thresholding

Interview questions

QWhat kind of data does BernoulliNB model?

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.

QHow does BernoulliNB handle non-binary inputs?

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.

QHow does its decision rule differ from MultinomialNB?

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.

  • MultinomialNB
  • CategoricalNB
  • ComplementNB
  • GaussianNB
  • Naive Bayes

Source: BernoulliNB — scikit-learn 1.9.1 documentation. Checked 2026-10-15.