A multinomial naive Bayes spam filter uses the vocabulary {win,cash,meeting}. Training: spam documents "win win cash" and "cash cash"; ham documents "meeting win" and "meeting meeting cash". Using Laplace (add-one) smoothing, classify the new document "win meeting meeting". Why is the smoothing needed?

Supervised learning, Naive Bayes
Level L2 Elementary. Length Medium. Nature Mat Mathematical.

What's being asked

Whether you can estimate word probabilities from counts with smoothing and compare class scores.

Key idea

P^(w∣c)=Ncw+1Nc+∣V∣ and the score is P(c)∏w∈docP^(w∣c), one factor per word occurrence.

Steps

  1. Priors: 2 spam and 2 ham documents, so P(spam)=P(ham)=0.5.
  2. Word counts. Spam: win 2, cash 3, meeting 0 (total Ns=5). Ham: win 1, cash 1, meeting 3 (total Nh=5). The vocabulary size is ∣V∣=3.
  3. Smoothed spam probabilities (denominator 5+3=8): win 3/8, cash 4/8, meeting 1/8. Ham: win 2/8, cash 2/8, meeting 4/8.
  4. Scores for "win meeting meeting": spam score is 0.5×38×18×18=0.5×3512≈0.00293, and ham score is 0.5×28×48×48=0.5×32512=0.03125.
  5. Ham wins. The posterior of ham is 0.031250.03125+0.00293=3235≈0.914.
  6. Smoothing is needed because "meeting" never occurs in spam. Without it P^(meeting∣spam)=0 and the spam score would be exactly 0 for any document containing "meeting", however spammy its other words are.

Common mistakes

How long your answer should be

Half a page; 4–5 minutes.

Read this

Previous in Supervised learning: The training points are A=(1,1) red, B=(2,1) red, C=(4,5) blue, D=(5,4) blue and E=(4,2) blue. Using Euclidean distance, classify the query q=(3,2) with k=1, k=3 and k=5. What does this tell you about the choice of k?
Next in Supervised learning: Four observations are x=1.5, 2, 4, 7. What is the estimated density at x=3 using (a) a box kernel of width 2h=4, that is K(u)=12 for ∣u∣≤1 with h=2, and (b) a Gaussian kernel with h=1?