Sub topic 15
Limitations, and why naive Bayes still works
This chapter assumes you know the naive Bayes decision rule, how smoothing works, and why we sum log probabilities. It covers where the model breaks and why it often classifies well anyway.
The assumption is usually false
Naive Bayes scores a class by multiplying one likelihood per feature, as if features were independent once the class is known. Real features rarely are. In text, "new" and "york" appear together far more often than chance. In a medical dataset, weight and blood pressure move together within every diagnosis.
The model has no term for these relationships, so it cannot learn them. That causes two kinds of problems: overconfident probabilities, and an inability to represent interactions.
Failure mode 1: double counting evidence
Suppose two features carry the same information. The model treats them as two independent pieces of evidence and adds both log likelihood ratios. For two classes, the log-odds are:
log P(y=1|x) / P(y=0|x) = log prior ratio + sum of log likelihood ratios
If a feature appears k times (or k copies of it exist), its contribution is multiplied by k. The following code shows the effect on the posterior when a word with a likelihood ratio of 4 to 1 is counted several times, with equal priors.
import math
p_spam, p_ham = 0.8, 0.2 # P(word | spam), P(word | ham)
for copies in (1, 2, 5):
log_odds = copies * math.log(p_spam / p_ham)
posterior = 1 / (1 + math.exp(-log_odds))
print(copies, round(posterior, 4))The output is 1 0.8, 2 0.9412, and 5 0.999. Nothing new was learned between copy one and copy five, yet the model went from 80% to 99.9% sure. This is why naive Bayes probabilities drift toward 0 and 1.
Failure mode 2: no feature interactions
Because each feature contributes its own separate term, the model cannot represent "A matters only when B is present". The classic case is XOR, where the class is 1 when exactly one of two binary features is 1.
from sklearn.naive_bayes import BernoulliNB
X = [[0, 0], [0, 1], [1, 0], [1, 1]]
y = [0, 1, 1, 0]
model = BernoulliNB(alpha=1.0)
model.fit(X, y)
print("accuracy:", model.score(X, y))
print(model.predict_proba([[0, 1]]))Each feature on its own is equally likely in both classes, so every likelihood ratio is 1. The model scores both classes identically, assigns probability 0.5 to each, and reaches only 0.5 accuracy, even on its own training data. A decision tree or a model with an interaction feature solves this problem easily.
Why it still works: ranking, not probabilities
A classifier needs only the correct argmax. The posterior probabilities can be badly wrong as long as the right class gets the highest score.
In the duplication example, extra copies pushed the posterior from 0.8 to 0.999, but the predicted class stayed "spam" throughout. Overconfidence changes the size of the log-odds and leaves their sign alone.
Domingos and Pazzani (1997) analyzed this formally. Naive Bayes can be optimal under zero-one loss (plain error rate) even when features are strongly dependent. Their conditions include cases where dependencies are spread similarly across classes and cancel in the comparison, and cases where one class dominates by a wide margin so that moderate errors do not flip the result.
Three practical reasons also help:
- Low variance. The model estimates one small distribution per feature per class, not a joint distribution over all features. With limited data, a slightly biased model with few parameters often beats a flexible one that overfits.
- Many weak signals. In bag-of-words text, thousands of features each add a little evidence. Overcounting inflates the margin but rarely reverses it.
- Fast, stable training. Training is a single pass of counting, with no iterative optimization to tune or fail to converge.
Other limitations to watch for
| Limitation | Symptom | Common mitigation |
|---|---|---|
| Correlated features | Overconfident probabilities, bias toward the group of correlated features | Remove redundant features, select features, or calibrate outputs |
| Unseen feature value in a class | Zero likelihood wipes out the whole score | Laplace smoothing with `alpha > 0` |
| Non-Gaussian continuous features | Poor fit from `GaussianNB` | Transform features, bin them, or use another model |
| Class imbalance | The prior dominates, minority class is under-predicted | Adjust priors, evaluate with recall and F1, try complement naive Bayes |
| Feature interactions | Systematic errors that more data does not fix | Add interaction features or choose a different model |
| Very long documents | Many repeated words inflate scores | Use binary or TF-IDF features, and check the effect on validation data |
Common errors
- Trusting the probabilities for decisions. Using a 0.9 cutoff on raw output assumes calibration that the model does not have.
- Keeping duplicated features. Adding the same signal under several names, such as raw count, log count, and a flag, multiplies its influence.
- Concluding that high accuracy proves the assumption holds. Accuracy only shows that the ranking is good enough on that data. It says nothing about the independence assumption.
- Stopping at accuracy. On imbalanced data, a model that always predicts the majority class scores well. Check per-class precision and recall.
- Expecting more data to fix interaction errors. Bias from the model form stays, no matter how many rows you add.
Decision criteria
Choose naive Bayes when you need a fast baseline, have little labeled data, have very high-dimensional sparse features such as text, or need to retrain often. Move to logistic regression, trees, or boosting when you suspect strong interactions, need trustworthy probabilities, or when a baseline comparison shows a clear accuracy gap. Logistic regression is the natural comparison: it uses the same linear log-odds form but learns weights jointly, so correlated features share credit instead of each claiming all of it.
Exercise
A classifier built with MultinomialNB gives 97% accuracy on a held-out set, yet its predict_proba values are almost always above 0.99 or below 0.01, including on examples it gets wrong. Explain why both observations can be true, and name one step you would take before using a 0.95 probability threshold in production.
Show answer
Accuracy depends only on which class has the highest score, and overcounting correlated features inflates the log-odds without changing their sign. The model therefore ranks classes correctly while its probabilities are pushed toward 0 and 1 and are overconfident, including on mistakes. Before using a 0.95 threshold, calibrate the probabilities (for example with CalibratedClassifierCV using isotonic or sigmoid calibration) and check a reliability curve on held-out data, or choose the threshold directly from validation precision and recall instead of from the raw probability.