Naïve Bayes
Independence assumption, Gaussian NB, Laplace smoothing
An email arrives: "Claim your FREE prize NOW." You need to classify it as spam or ham in milliseconds. You have word frequency statistics from training: P(FREE|spam) = 0.45, P(FREE|ham) = 0.01, P(prize|spam) = 0.32, P(prize|ham) = 0.002, P(Claim|spam) = 0.18, P(Claim|ham) = 0.04. Bayes' theorem gives the posterior: P(spam|words) ∝ P(spam) × P(FREE|spam) × P(prize|spam) × P(Claim|spam) × .... You multiply across all words in the email. Whichever class — spam or ham — gives the larger product wins.
The "naive" assumption is that words are conditionally independent given the class. This is obviously false. "Stock" and "market" co-occur constantly. "Credit" and "card" cluster together. The joint P(stock, market|spam) is nothing like P(stock|spam) × P(market|spam). The model is provably wrong about the joint distribution. Yet it works.
The reason: you do not need the correct probability, only the correct ranking. Is P(spam|words) > P(ham|words)? Naive Bayes gets the ordering right even when the individual probabilities are wrong, because the errors in the independence assumption tend to be symmetric — both classes' probabilities are over-estimated by roughly the same factor.
One failure mode is deterministic. A word not seen in any training spam email has P(word|spam) = 0. One unseen word → the entire product P(x|spam) = 0 → P(spam|x) = 0 → the model can never classify any email containing that word as spam, regardless of all other evidence. A single "zarflax" in the email makes it immune to spam classification. Laplace smoothing fixes this: add 1 to all word counts before computing probabilities, making P(new_word|spam) = 1/(n_spam + vocab_size). Never exactly zero.
The three variants handle different data types. Multinomial NB uses word counts, treating each email as a bag of word draws from a class-conditional multinomial — the right choice for text. Bernoulli NB uses word presence or absence, ignoring count information — faster but less informative. Gaussian NB models continuous features as Gaussian distributions per class, fitting one mean and one variance per feature per class. For the spam filter, Multinomial NB is correct. For a dataset with continuous medical measurements, Gaussian NB is correct. Applying Gaussian NB to text, or Multinomial NB to continuous features, produces silently wrong models.
NOT this. Naive Bayes is too simple for real use. For high-dimensional sparse features — text, categorical bags — Naive Bayes is competitive with SVMs and logistic regression while training in milliseconds on a single pass through the data. With 10,000 vocabulary items and 100 training documents, logistic regression has 10,000 parameters to estimate and overfits severely even with strong regularization. Naive Bayes has 10,000 simple count estimates that are stable at any sample size. It is still deployed in production spam filters and intent classifiers where training speed, interpretability, and stability with small data matter more than 1–2 percentage points of accuracy against a tuned neural classifier.
The formal statement: ŷ = argmax_k [log P(y=k) + Σⱼ log P(xⱼ|y=k)]. Always compute in log-space. Multiplying thousands of probabilities like 0.001 causes floating-point underflow to exactly zero before the product completes. Log-space turns the product into a sum, eliminating underflow entirely.
Key points
- Use Gaussian NB for continuous features as a fast, interpretable baseline — it trains in O(nd) and gives a probability estimate. If it performs well, there may not be complex feature interactions worth modeling. For a medical dataset with 20 continuous features and 500 patients, Gaussian NB trains in one pass — compute the mean and variance of each feature for each class. Prediction: for a new patient, compute the Gaussian log-likelihood of each feature value under each class's distribution, sum the logs, add the log prior, take the argmax. No gradient descent, no hyperparameter tuning. If Gaussian NB achieves AUC 0.78 and a tuned gradient boosted tree achieves 0.82, the marginal value of the complex model is 4 points — often not worth the engineering cost and opacity.
- Trap: forgetting Laplace smoothing. A single unseen word in a test document zeroes out the entire posterior. Always use α > 0 (sklearn default α = 1). For the spam filter: training vocabulary is 50,000 words. A new test email contains the word "cryptocurrency" which appeared in no training spam examples. Without smoothing: P(cryptocurrency|spam) = 0. The entire P(x|spam) product becomes 0. P(spam|x) = 0. No matter how many other spam-indicating words appear — FREE, prize, Claim, urgent — the email will never be classified as spam. With Laplace smoothing: P(cryptocurrency|spam) = 1 / (n_spam_tokens + 50,000) ≈ 0.00002. Small but nonzero. The other spam signals dominate. The email is correctly classified as spam.
- Diagnostic: if Naive Bayes predicts near 0.0 or 1.0 with very high confidence on most examples, the independence assumption is badly violated and the probabilities are not calibrated — use the predictions for ranking only, not as probabilities. For the spam filter: if 90% of test emails get P(spam) > 0.999 or P(spam) < 0.001, the model is over-counting correlated evidence. "FREE" and "prize" and "WIN" all appear together in spam — each one multiplies the spam probability by a large factor, but those factors are not independent signals. The product over-saturates. In the reliability diagram, predictions near 1.0 correspond to actual spam rates of only 0.75. Apply Platt scaling: fit a logistic regression on the NB log-odds using a held-out calibration set. The ranking stays correct; the probabilities become honest.
Naive Bayes only needs to rank P(spam|words) above P(ham|words) correctly — not to get the individual probabilities right — and the independence assumption fails symmetrically enough that the ranking holds even when the probabilities saturate toward 0 and 1.
Recap
- Naive Bayes multiplies P(feature|class) across features via Bayes' theorem; larger product wins.
- Only needs to rank P(spam|words) > P(ham|words) — not to get individual probabilities right.
- Independence assumption fails symmetrically enough that the ranking survives even as probabilities saturate to 0/1.
- Laplace smoothing is mandatory — one unseen word zeroes the whole posterior; use α > 0 (sklearn default α = 1).
- Gaussian NB for continuous features — fast O(nd) baseline.
- If it predicts near 0/1 with high confidence, independence is badly violated — use for ranking only, not as probabilities.
Check your understanding
Q1. In Multinomial NB for spam detection, a test email contains the word "win" which appears in 0% of spam emails in training. Without Laplace smoothing, select the two true statements about what happens.
- `A) Since NB multiplies all feature probabilities, the whole product P(x|spam) becomes exactly 0, so the email can never be classified as spam regardless of other words.`
- `B) Laplace smoothing adds α=1 to counts so P("win"|spam) becomes small but non-zero, letting the other, genuinely spammy words still decide the classification.`
- `C) P("win"|spam)=0 gets skipped as a feature entirely, so the classifier proceeds using only the remaining words in the email to reach its final decision.`
- `D) The zero probability only makes the log-posterior for spam negative infinity while ham's stays finite, so the model classifies it as ham by strict default.`
Q2. Why does Naïve Bayes often outperform logistic regression on text classification when training data is small?
- `A) NB benefits purely from words being actually independent in real text, which makes it the mathematically optimal Bayesian classifier whenever that holds true.`
- `B) Logistic regression's gradient descent converges slowly on sparse text vectors while NB's closed-form counting reaches its optimum in a single pass always.`
- `C) NB's independence assumption acts as strong regularisation, preventing it from memorising training-specific word co-occurrence patterns that LR would overfit.`
- `D) NB estimates generative per-class word counts, which stay stable with tiny n; LR must fit one weight per word discriminatively and overfits in high dimensions with little data.`
Q3. Your Naïve Bayes classifier outputs P(spam)=0.99 for an email. How confident should you be, and what would you do if calibrated probabilities are required?
- `A) P(spam)=0.99 is reliable exactly as a probability — use it directly as a confidence score and set the threshold from the acceptable false-positive rate alone.`
- `B) NB probabilities are well-calibrated for balanced binary classification specifically; the independence bias toward 0 and 1 only shows up in multiclass problems.`
- `C) NB posteriors are poorly calibrated, converging toward 0/1 faster than truth. Fix with Platt scaling or isotonic regression on a separate calibration set.`
- `D) The 0.99 is overconfident purely from double-counting repeated words; switching to a binary bag-of-words alone removes the bias with no calibration step needed.`
Try it interactively
ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.
Open ML Systems Lab →