Skip to content
AI.info

Classical machine learning

Naive Bayes Classifiers

Compare Gaussian, multinomial, Bernoulli, and complement Naive Bayes with smoothing, calibration, correlated evidence, and online updates.

By the end you can

Visual

Naive Bayes combines prior prevalence with feature evidence

The model applies Bayes rule after factorizing class-conditional feature likelihoods.

Everything that follows in this lesson is an argument about the third box. What the factorization costs, when it costs nothing at all, and what it quietly does to the fourth.

FigureProcess · 5 steps
  1. 1

    Class prior

    Start with the expected prevalence of each class.

  2. 2

    Feature likelihoods

    Estimate how compatible each observed feature is with each class.

  3. 3

    Conditional-independence factorization

    Multiply feature contributions given the class.

  4. 4

    Posterior score

    Combine prior and evidence, usually in log space.

  5. 5

    Prediction

    Choose a class or pass scores to a calibrated decision policy.

The naive assumption is conditional independence, not ordinary independence

The model assumes features are independent of one another after the class is known. Two words can be strongly associated overall and still satisfy the modeling approximation differently within spam and non-spam classes.

Real features violate the assumption, and the classifier still wins. Domingos and Pazzani measured how often, in 1997. They ran the simple Bayesian classifier against C4.5, PEBLS and CN2 on 28 UCI datasets, 20 random 2/3–1/3 splits each. It was more accurate than C4.5 in 19 domains against 9, than PEBLS in 16 against 11, and than CN2 in 20 against 8. Average accuracy 79.1% against 77.2%, 77.6% and 76.2%, and the best average rank of all five learners at 2.43. Then they did the part that decides the question. They measured attribute dependence directly and correlated it with the classifier's accuracy advantage. The correlation was essentially nil: R² = 0.04 against C4.5, 0.0004 against PEBLS, 0.002 against CN2. The classifier was not winning because the independence assumption happened to hold. Their abstract states where the two kinds of success part company: “The region of quadratic-loss optimality of the Bayesian classifier is in fact a second-order infinitesimal fraction of the region of zero-one optimality.” Getting the class right and getting the probability right are different regions. The second is vanishingly smaller than the first.

The other half of the explanation is how little data the simplified factorization needs. Ng and Jordan proved the rate in 2001. Naive Bayes converges to its higher asymptotic error after O(log n) training examples, where logistic regression needs O(n), with n the number of features. Their abstract states the trade in one line — “while discriminative learning has lower asymptotic error, a generative classifier may also approach its (higher) asymptotic error much faster.” They confirmed the resulting two-regime crossover on 15 UCI datasets, 8 continuous and 7 discrete, averaged over 1,000 random train/test splits. Zheng and colleagues generalised the same O(log n) versus O(n) result from the binary case to the multiclass one in 2023. The naive model is not a good approximation of the world. It is a cheap one, and cheapness is worth most when labels are scarce.

The violated assumptions do cost accuracy, and most of that cost can be repaired. On the Industry Sector corpus multinomial Naive Bayes reached 0.582 accuracy. A transformed weight-normalized complement variant reached 0.923, against 0.934 for a support vector machine. On 20 Newsgroups the same three numbers were 0.848, 0.861 and 0.862. Rennie and colleagues published those figures in 2003. What closed most of the gap was not a better probability model. It was a set of corrections to the representation: weights estimated from the data outside each class, normalized document length, damped repeated terms. The authors note that the result “no longer has a generative interpretation”.

Figure

The corrections recover almost all of the gap on both corpora, and what that is worth depends entirely on how large the gap was — thirty-four points on one corpus, one on the other. Rennie, Shih, Teevan and Karger, ICML 2003; both gaps, both closed shares and the ratio between them are derived from the published accuracies.

Comparison

Choose the likelihood model that matches the feature representation

Naive Bayes is a family rather than one universal estimator. The choice between its members is measurable rather than aesthetic.

The two text members are not equally good, and the gap has a number on it. McCallum and Nigam put them against each other in 1998, on five corpora. Yahoo Science (13,589 pages, 95 classes, vocabulary 44,383), Industry Sector (6,440 pages, 71 classes, vocabulary 29,964), 20 Newsgroups (about 20,000 articles, 20 groups, vocabulary 62,258), WebKB (4,199 pages, 4 classes, vocabulary 23,830) and the Reuters-21578 ModApte split (12,902 articles, 10 most populous classes, vocabulary 19,371). Their conclusion: “The multinomial model is found to be almost uniformly better than the multi-variate Bernoulli model. In empirical results on five real-world corpora we find that the multinomial model reduces error by an average of 27%, and sometimes by more than 50%.” Picking the wrong member of the family is worth about a quarter of your errors, and sometimes half of them.

The exception is where the useful rule lives. Bernoulli sometimes won at small vocabulary sizes. So the choice is not a default but a function of how many features the representation keeps. Introduction to Information Retrieval records the same division: multinomial “can handle more” features, Bernoulli “works best with fewer”. Wang and Manning report the gap independently in 2012, where multivariate Bernoulli NB “performs up to 10% worse” than multinomial NB. The comparison below is a decision procedure, not a ranking. Match the likelihood to how the features are actually encoded, then measure the choice on your own data.

FigureComparison · 4 columns

Gaussian NB

Models each continuous feature with a class-specific Gaussian distribution.

  • Means and variances per class
  • Sensitive to skew and outliers
  • Supports online updates
  • Ignores within-class correlations

Multinomial NB

Models nonnegative occurrence counts or count-like features.

  • Common in text classification
  • Uses class-specific token rates
  • Needs smoothing for unseen events
  • Document length affects evidence

Bernoulli NB

Models binary feature presence or absence.

  • Useful for boolean indicators
  • Absence can contribute evidence
  • Requires a binarization policy
  • Differs from count-based modeling

Complement NB

Estimates weights using data outside each class.

  • Often useful for imbalanced text
  • Designed to reduce some multinomial imbalance problems
  • Still relies on representation choices
  • Needs empirical comparison

Example

Why Naive Bayes remains a formidable text baseline

Sparse token counts align naturally with a simple generative story and fast updates. That alignment has a measured boundary rather than an open horizon.

Document length is the variable that decides it. On short snippets, multinomial Naive Bayes with bigrams scored 79.0 on RT-s, against 76.2 for a unigram SVM and 77.7 for a bigram SVM. It beat the SVM on every snippet dataset except one: “With the only exception being MPQA, MNB performed better than SVM in all cases”. Wang and Manning mapped the boundary in 2012. On full-length reviews the order reverses. On IMDB, bigram MNB scored 86.59 against 89.16 for the bigram SVM and 91.22 for their NBSVM hybrid. The authors name the cause rather than the symptom: “Compared to the excellent performance of MNB on snippet datasets, the many poor assumptions of MNB pointed out in (Rennie et al., 2003) become more crippling for these longer documents.” NBSVM was later reproduced on IMDB at 91.87% with trigrams, with Wang and Manning's 91.22% cited as the figure to beat. The same classifier that leads on a sentence trails on a review.

  • Routing: token frequencies can distinguish billing, technical, and account-support messages with limited labeled data — the O(log n) regime Ng and Jordan describe.
  • Spam filtering: rare but class-specific words can contribute strong evidence after smoothing.
  • Language identification: character n-gram counts provide robust signals for short strings, which is the length regime where MNB scored 79.0 on RT-s against the bigram SVM's 77.7.
  • Sentiment limitation: on full-length IMDB reviews the same bigram MNB scored 86.59 against 89.16 for the bigram SVM, because correlated phrases and negation accumulate over a long document.
  • Probability limitation: repeated correlated tokens can make posterior scores excessively confident, which is a separate failure from getting the class wrong.

Smoothing prevents zero likelihood from erasing every other clue

Without smoothing, one feature never observed in a class can assign that class zero likelihood for the entire row. Additive smoothing reserves probability mass for unseen events.

That mass has to be taken from somewhere, and the bill is itemised. Jurafsky and Martin work the arithmetic in Speech and Language Processing, on the Berkeley Restaurant Project corpus of 9,332 sentences with a vocabulary of V = 1,446. Add-one smoothing cuts the bigram count C(want to) from 608 to 238, and P(to|want) from 0.66 to 0.26 — a discount of 0.39. On the same corpus the discount for “Chinese food” is 0.10. A factor of ten apart. Their explanation: “The sharp change occurs because too much probability mass is moved to all the zeros.” The same chapter's verdict is that “Laplace smoothing does not perform well enough to be used in modern n-gram models”. Chen and Goodman had reached that verdict empirically in 1996, comparing smoothing methods across corpora, training sizes and n-gram orders: “From these graphs, we see that additive smoothing performs poorly and that methods katz and interp-held-out consistently perform well.”

Too much smoothing washes out genuine differences, and too little produces brittle estimates. The sharper point is that the discount is not applied evenly. Two estimates on one corpus were charged 0.39 and 0.10 for the same repair. Tune the strength within a valid pipeline and data regime. Then look at what it did to the specific estimates your decision depends on, rather than at the average.

Analogy

Combining clues as if witnesses had not spoken to one another

A detective collects several witness statements. He multiplies their evidential strength, assuming each witness formed an opinion independently once the true scenario is fixed.

The multiplication is wrong where witnesses copied one another or watched the same underlying clue. Correlated features can then make the model count evidence repeatedly. As Domingos and Pazzani's 28 datasets show, it will still name the right suspect while being badly wrong about how certain the case is.

Naive Bayes is efficient because it treats feature evidence as separable, even when that approximation is imperfect.

Steps

Build a Naive Bayes baseline that teaches you something

Naive Bayes is fast enough to support extensive representation checks.

Each step below has a measured quantity behind it in this lesson. The variant choice was worth an average 27% reduction in error to McCallum and Nigam. The smoothing strength moved P(to|want) from 0.66 to 0.26 in one worked example. And the calibration check is where the false independence assumption becomes visible, because that is the one place the ranking metrics will not show it to you.

FigureProcess · 6 steps
  1. 1. Match variant to features

    Choose continuous, count, binary, or complement likelihoods deliberately.

  2. 2. Keep transformations fold-aware

    Build vocabularies, binarization, and filtering within training folds.

  3. 3. Tune smoothing

    Search a plausible range using decision-relevant metrics.

  4. 4. Inspect class evidence

    Read the strongest feature contributions and duplicated signals.

  5. 5. Check calibration

    Compare scores with observed class frequencies.

  6. 6. Test correlated features

    Remove redundant groups and measure confidence and ranking changes.

Key idea

Accurate classification can coexist with poor probability estimates

Naive Bayes may place the correct class first while assigning extreme posterior values. The cause is correlated evidence multiplied repeatedly. Ranking, classification, and calibration should therefore be evaluated separately.

The distortion has a direction, and it is the opposite of the usual one. Niculescu-Mizil and Caruana catalogued it in 2005, across ten learning algorithms. Maximum-margin methods such as boosted trees and boosted stumps push probability mass away from 0 and 1, which gives a characteristic sigmoid-shaped reliability curve. Naive Bayes fails the other way: “Models such as Naive Bayes, which make unrealistic independence assumptions, push probabilities toward 0 and 1”, and “Because Naive Bayes makes the unrealistic assumption that the attributes are conditionally independent given the class, it tends to push predicted values toward 0 and 1”. Scikit-learn's calibration documentation says the same of one implementation — “GaussianNB (Naive Bayes) tends to push probabilities to 0 or 1 … because it makes the assumption that features are conditionally independent given the class”.

The distortion also has a size. In Introduction to Information Retrieval, true probabilities of 0.6 and 0.4 come back estimated as 0.99 and 0.01. Its phrase for the whole phenomenon is “NB classifiers estimate badly, but often classify well”. The argmax survived. The number attached to it did not. So do not publish the raw posterior as risk without validation.

A useful decision boundary does not guarantee a trustworthy probability scale.

Incremental updates are convenient but can hide distribution change

Several Naive Bayes variants update sufficient statistics without retaining all past rows. This makes them attractive for streams and large sparse datasets.

That regime has a canonical evaluation, and it is worth knowing what one looks like. The TREC 2005 Spam Track evaluated 53 filters on 8 corpora under a strictly online protocol. Forty-four of those filters were submitted by 12 participating groups. Messages arrive in chronological order, and the filter must classify each one before it is told the answer: “The gold standard for each message is communicated to the filter immediately following classification.” Cormack and Lynam ran the track under NIST's Text REtrieval Conference. The public corpus trec05p-1/full holds 92,189 messages: 39,399 ham and 52,790 spam, 57.3% spam.

The statistics still summarize a historical mixture. Update policies therefore need drift monitoring, decay or windows when old evidence should lose relevance. A chronological stream is exactly where that bites. Counts accumulated over 92,189 messages describe the traffic that has already arrived, and nothing in the update rule notices when the traffic changes underneath them.

Fast updating does not answer how much history the model should remember.

Key takeaways