Part IV — Supervised Learning: Regression & Linear Classifiers · Chapter 10

The naive Bayes classifier

Hook

Part I's Bayes' rule updated a belief given one piece of evidence. What happens with several pieces of evidence at once — and what's the cheapest possible assumption that makes combining them tractable?

Intuition

Six training messages, three spam, three not. Toggle smoothing on a new message containing "free" and "money" — both toggles predict spam, but look at how confident each one is. One of them is lying about how sure it should be.

Formalize

Naive Bayes multiplies Bayes' rule across every feature, assuming they're independent given the class — the "naive" part:

P(classx1,,xn)P(class)i=1nP(xiclass)P(\text{class} \mid x_1,\dots,x_n) \propto P(\text{class}) \prod_{i=1}^n P(x_i \mid \text{class})
  • class — the category being predicted, such as spam or not-spam.
  • x1,,xnx_1,\dots,x_n — the nn individual pieces of evidence, such as which words are present.
  • P(class)P(\text{class}) — the prior probability of the class, before looking at any evidence.
  • P(xiclass)P(x_i \mid \text{class}) — the likelihood of feature ii: how often it shows up in training examples of that class.
  • nn — the number of features being combined.
  1. Each likelihood is just a frequency

    P(xiclass)P(x_i \mid \text{class}) is how often word ii showed up in training messages of that class — a simple count, nothing more elaborate.

  2. Independence turns a hard problem into easy ones

    Multiplying independent probabilities together is exactly what "assume independence" means — it's the one assumption that turns a hard joint-probability problem into a handful of easy word-counting ones.

Play

Build a message by toggling which words are present. "Meeting" alone should pull hard toward not-spam; "free" alone pulls the other way. Watch how the posterior shifts as you add or remove one piece of evidence at a time — each word's presence (or absence) multiplies in independently.

Worked example

"Free" never appears in any of the three not-spam training messages:

  1. The zero-frequency problem

    P(freenot spam)=0/3=0P(\text{free}\mid\text{not spam}) = 0/3 = 0 exactly. For a message containing "free," this makes the entire not-spam probability collapse to precisely 00 — not small, not unlikely, but mathematically impossible — purely because of one gap in a tiny training set.

  2. Laplace smoothing patches it

    Add one fake "seen" and one fake "not seen" example to every count: P(freenot spam)=(0+1)/(3+2)=0.2P(\text{free}\mid\text{not spam}) = (0+1)/(3+2) = 0.2. Never exactly zero again, no matter how sparse the training data.

  3. Same prediction, honest confidence

    For "free, money" (meeting absent), each class's unnormalized score is its prior times P(freec)×P(moneyc)×(1P(meetingc))P(\text{free}\mid c) \times P(\text{money}\mid c) \times (1-P(\text{meeting}\mid c)):

    Unsmoothed:

    • Spam: 0.5×23×23×(10)0.2220.5 \times \frac23 \times \frac23 \times (1-0) \approx 0.222
    • Not spam: 0.5×0×13×(123)=00.5 \times 0 \times \frac13 \times (1-\frac23) = 0 — the zero-frequency word forces the whole product to 00

    Normalizing: spam 0.222/0.222=100%\approx0.222/0.222=100\%, not spam =0%=0\% — absolute certainty from one missing example.

    Smoothed:

    • Spam: 0.5×0.6×0.6×(10.2)=0.1440.5 \times 0.6 \times 0.6 \times (1-0.2) = 0.144
    • Not spam: 0.5×0.2×0.4×(10.6)=0.0160.5 \times 0.2 \times 0.4 \times (1-0.6) = 0.016

    Normalizing: spam =0.144/0.16=90%=0.144/0.16=90\%, not spam =0.016/0.16=10%=0.016/0.16=10\% — the same correct prediction, but at a far more reasonable confidence.

Checkpoint

Find the setting where the model claims more than 99% certainty — from a single missing training example, not real evidence.

Toggle smoothing to try it
Summary
P(classx1,,xn)P(class)i=1nP(xiclass)P(\text{class} \mid x_1,\dots,x_n) \propto P(\text{class}) \prod_{i=1}^n P(x_i \mid \text{class})

The independence assumption is almost always technically false — whether an email says "free" and whether it says "money" are not really independent — and naive Bayes works remarkably well anyway, because getting the relative ranking between classes right matters far more than getting the exact probabilities right. Laplace smoothing isn't a minor implementation detail; without it, any word that happens to be missing from one class's tiny training set can make an entire prediction mathematically impossible, forever.