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}d:
p^=P(y=1), the prior probability of spam;
p^j1=P(xj=1∣y=1), the probability that word j appears in spam;
p^j0=P(xj=1∣y=0), the probability that word j appears in genuine mail.
Each is a coin-flip probability, and its maximum likelihood estimate is a frequency:
p^=n#{spam emails},p^j1=#{spam emails}#{spam emails containing word j},
and likewise for p^j0 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 x, Bayes' rule with the independence assumption gives
P(y=1∣x)∝p^j=1∏d(p^j1)xj(1−p^j1)1−xj,
and similarly for y=0 with 1−p^ and p^j0. 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:
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".
Suppose the word "invoice" appeared in some genuine emails but in none of the spam emails in the training set. Then p^invoice1=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^=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^jy=#{class-y emails}+2#{class-y emails containing word j}+1.
This is Laplace smoothing (add-one smoothing), the posterior mean under a flat 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 α instead of 1 to each count, with α chosen by validation.
The decision function is linear
Compare the two log scores: predict spam when score1(x)>score0(x). Collect the terms that multiply each xj:
a linear classifier. Each word casts a vote wj 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 b 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). They differ in how they choosew: naive Bayes fits each class's data separately and derives the boundary; logistic regression chooses w 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.
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.