Multinomial and Bernoulli naive Bayes
Quick answer
Multinomial naive Bayes uses nonnegative feature counts, while Bernoulli naive Bayes models whether each feature is present or absent. The right choice depends on how observations are represented. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Contrast count and presence features
from sklearn.feature_extraction.text import CountVectorizerv=CountVectorizer(vocabulary=["alarm","quiet"])x=v.transform(["alarm alarm quiet","unknown"]).toarray()print(x.tolist())print((x>0).astype(int).tolist())Expected output:
[[2, 1], [0, 0]][[1, 1], [0, 0]]The repeated token is counted twice but marked present once; unknown words are ignored.
How it works
For vocabulary size V, multinomial counts n_cj record total occurrences of token j within class c. With additive smoothing alpha, theta_cj=(n_cj+alpha)/(sum_j n_cj+alphaV). A document vector x has log score log P(c)+sum_j x_j log theta_cj; zero counts contribute nothing. Bernoulli NB instead counts documents containing each term. With N_c documents and m_cj containing term j, theta_cj=(m_cj+alpha)/(N_c+2alpha). Its score includes x_j log theta_cj + (1-x_j) log(1-theta_cj) for binary x. Thus absence can count as evidence, unlike the multinomial model. Use explicit binary inputs and binarize=None when they are already encoded. Fit a CountVectorizer only on training text, then transform held-out text with the same vocabulary. Unseen words produce no feature; an all-unknown document maps to zeros. Multinomial scores then reduce to class priors; Bernoulli still incorporates absence of every known term. An entirely empty fitted vocabulary is different and should be reported rather than fabricating columns. Negative standardized counts are incompatible with MultinomialNB. Word repetition changes counts but not presence. Neither likelihood family guarantees reliable probabilities when terms are dependent.
Fit both likelihood families
from sklearn.naive_bayes import MultinomialNB,BernoulliNBX=[[3,0],[2,0],[0,3],[0,2]]; y=[0,0,1,1]print(MultinomialNB(alpha=1).fit(X,y).predict([[2,0],[0,2]]).tolist())B=[[int(v>0) for v in r] for r in X]print(BernoulliNB(alpha=1,binarize=None).fit(B,y).predict([[1,0],[0,1]]).tolist())Expected output:
[0, 1][0, 1]Both agree on these clear queries, but their scoring rules differ.
Common mistakes
Do not fit a vocabulary on the full corpus or pass negative standardized counts to MultinomialNB. Do not confuse total token counts with document presence counts. Treat empty vocabulary and unknown-only documents separately.
Practice and next steps
For documents [alarm alarm, alarm quiet], count occurrences and document presences of each term. Explain why a missing term changes a Bernoulli score. Complete the knowledge check and coding exercise. Next: Smoothing and log likelihood.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Multinomial and Bernoulli naive Bayes — knowledge check
Apply multinomial and bernoulli naive bayes to new examples.
Which feature representation matches Bernoulli NB?
How does multinomial NB treat zero term count?
What happens to unknown words with a frozen vocabulary?
What differs in the Bernoulli score?
What is an empty fitted vocabulary?