Naive Bayes

Naive Bayes estimates each class's prior and per-feature probabilities by counting, then classifies with Bayes' rule in log space. A word never seen in one class gets probability zero and vetoes that class, which Laplace smoothing fixes by adding pseudo-counts. For binary features its decision function is linear: a weighted vote of log-odds, one per feature.

Machine Learning Techniques

The last chapter set up the generative story for classifying emails and showed that it is only practical with an assumption: given the class, the words appear independently. This chapter turns that assumption into a working classifier, fixes its one serious flaw, and shows that its decision rule is, surprisingly, a straight line.

Training by counting

The model has three kinds of parameters for a binary problem with binary features x∈{0,1}dx \in \{0, 1\}^d:

  • p^=P(y=1)\hat p = P(y = 1), the prior probability of spam;
  • p^j 1=P(xj=1∣y=1)\hat p^{\,1}_j = P(x_j = 1 \mid y = 1), the probability that word jj appears in spam;
  • p^j 0=P(xj=1∣y=0)\hat p^{\,0}_j = P(x_j = 1 \mid y = 0), the probability that word jj appears in genuine mail.

Each is a coin-flip probability, and its maximum likelihood estimate is a frequency:

p^=#{spam emails}n,p^j 1=#{spam emails containing word j}#{spam emails},\hat p = \frac{\#\{\text{spam emails}\}}{n}, \qquad \hat p^{\,1}_j = \frac{\#\{\text{spam emails containing word } j\}}{\#\{\text{spam emails}\}},

and likewise for p^j 0\hat p^{\,0}_j using the genuine emails. Training is a single pass over the data, counting. That speed, and the ability to update the counts as new mail arrives, made naive Bayes the first widely deployed spam filter.

Predicting

For a new email xx, Bayes' rule with the independence assumption gives

P(y=1∣x)  ∝  p^∏j=1d(p^j 1)xj(1−p^j 1)1−xj,P(y = 1 \mid x) \;\propto\; \hat p \prod_{j=1}^{d} \big(\hat p^{\,1}_j\big)^{x_j}\big(1 - \hat p^{\,1}_j\big)^{1 - x_j},

and similarly for y=0y = 0 with 1−p^1 - \hat p and p^j 0\hat p^{\,0}_j. Predict the class with the larger value. In practice, multiply thousands of small probabilities and the result underflows to zero, so compare log scores instead:

scorey(x)=ln⁡P(y)+∑j=1d[xjln⁡p^j y+(1−xj)ln⁡(1−p^j y)].\text{score}_y(x) = \ln P(y) + \sum_{j=1}^{d} \Big[x_j \ln \hat p^{\,y}_j + (1 - x_j)\ln\big(1 - \hat p^{\,y}_j\big)\Big].

A tiny example

Training data: 4 spam and 6 genuine emails. The word "lottery" appears in 3 of the 4 spam emails and 1 of the 6 genuine ones; "meeting" appears in 1 spam and 4 genuine. A new email contains "lottery" but not "meeting".

spam:0.4×34×(1−14)=0.4×0.75×0.75=0.225genuine:0.6×16×(1−46)=0.6×0.167×0.333=0.033\begin{aligned} \text{spam:}\quad & 0.4 \times \tfrac{3}{4} \times \big(1 - \tfrac14\big) = 0.4 \times 0.75 \times 0.75 = 0.225 \\ \text{genuine:}\quad & 0.6 \times \tfrac{1}{6} \times \big(1 - \tfrac46\big) = 0.6 \times 0.167 \times 0.333 = 0.033 \end{aligned}

Normalising, P(spam∣x)=0.225/(0.225+0.033)≈0.87P(\text{spam} \mid x) = 0.225 / (0.225 + 0.033) \approx 0.87. Spam.

The zero-count problem

Suppose the word "invoice" appeared in some genuine emails but in none of the spam emails in the training set. Then p^invoice 1=0\hat p^{\,1}_{\text{invoice}} = 0, and any new email containing "invoice" gets spam probability exactly zero, no matter how many other spam words it contains: one factor of zero wipes out the whole product. A spammer only has to include one word that never happened to appear in the training spam. The same thing happens for a word that appears in every email of a class (then 1−p^=01 - \hat p = 0 for emails without it).

This is the overconfidence of maximum likelihood on small counts, met in Module 5 with three heads in three flips. The fix is the same one: a prior.

Laplace smoothing

Pretend that, before seeing any data, we saw every word appear once and not appear once in each class. The estimates become

p^j y=#{class-y emails containing word j}+1#{class-y emails}+2.\hat p^{\,y}_j = \frac{\#\{\text{class-}y\text{ emails containing word } j\} + 1}{\#\{\text{class-}y\text{ emails}\} + 2} .

This is Laplace smoothing (add-one smoothing), the posterior mean under a flat Beta(1,1)\text{Beta}(1, 1) prior. No estimate can now be exactly 0 or 1, so no single word can veto a class. With plenty of data the added counts make almost no difference; with sparse counts they keep the model sensible. A more general version adds α\alpha instead of 1 to each count, with α\alpha chosen by validation.

The decision function is linear

Compare the two log scores: predict spam when score1(x)>score0(x)\text{score}_1(x) > \text{score}_0(x). Collect the terms that multiply each xjx_j:

score1(x)−score0(x)=ln⁡p^1−p^+∑jln⁡1−p^j 11−p^j 0⏟b  +  ∑j=1dln⁡p^j 1 (1−p^j 0)p^j 0 (1−p^j 1)⏟wj  xj.\text{score}_1(x) - \text{score}_0(x) = \underbrace{\ln\frac{\hat p}{1 - \hat p} + \sum_j \ln\frac{1 - \hat p^{\,1}_j}{1 - \hat p^{\,0}_j}}_{b} \;+\; \sum_{j=1}^{d} \underbrace{\ln\frac{\hat p^{\,1}_j\,(1 - \hat p^{\,0}_j)}{\hat p^{\,0}_j\,(1 - \hat p^{\,1}_j)}}_{w_j}\; x_j .

So naive Bayes predicts spam exactly when

w⊤x+b>0:w^\top x + b > 0 :

a linear classifier. Each word casts a vote wjw_j equal to its log-odds ratio: positive if the word is relatively more common in spam, negative if more common in genuine mail, near zero if it is equally common in both. The intercept bb carries the prior and the baseline. This makes naive Bayes easy to interpret: the largest positive weights are the "spammiest" words.

It also places naive Bayes in the same family as logistic regression and the perceptron, which also predict with sign⁡(w⊤x+b)\operatorname{sign}(w^\top x + b). They differ in how they choose ww: naive Bayes fits each class's data separately and derives the boundary; logistic regression chooses ww directly to fit the labels. When the independence assumption is badly wrong (for example, a duplicated feature gets counted twice), naive Bayes's weights are distorted while logistic regression adjusts for the redundancy.

A horizontal bar chart of words with their log-odds weights: lottery, winner and free have large positive bars towards spam; meeting, invoice and agenda have negative bars towards genuine mail; and, the and is have bars near zero
Naive Bayes as a linear classifier. Each word's weight is its log-odds ratio between the classes; the email's score is the sum of the weights of the words it contains, plus a baseline.
MediumNaive BayesLaplace smoothing

Using the example above but with Laplace smoothing, recompute P(spam | email with 'lottery' and not 'meeting').

EasyNaive Bayes

Why does one unseen word break an unsmoothed naive Bayes classifier?

MediumNaive BayesLinear classifiers

Show that the naive Bayes weight for a word is zero when the word is equally common in both classes, and interpret the sign otherwise.