03 Text Classification - Chunk 04 (s.91-120)

Overview

This chunk finishes the first worked example of Concept - Naive Bayes (spam vs. normal messages) and then exposes its main weakness. The slides (a StatQuest-style “Double BAM” deck; each slide is one animation step) compute the scores for the message “Dear Friend” and conclude it is a normal message (0.09 > 0.01) (s.91-96). A second, “slightly more complicated” message “Lunch Money Money Money Money” is then classified (s.97-120). Intuitively it looks like spam because Money is much more probable in spam, but the spam score becomes exactly 0 because Lunch never appeared in a spam message in the training data. This is the Concept - Zero Probability Problem. The chunk ends by introducing the standard fix: add 1 count (a “black box”) to every word in every histogram, i.e. Concept - Laplace Smoothing (s.119-120).

Detailed notes

1. Setup carried over from earlier slides (visible on every slide s.91-120)

Probabilities estimated from the training data (word histograms) shown on the left of every slide:

Normal message (N)Spam (S)
Prior
0.470.29
0.290.14
0.180.00
0.060.57

(Blue envelopes = normal messages, red envelopes = spam; s.91-120. The earlier chunk derived these from word counts; see 03 Text Classification - Chunk 03 (s.61-90).)

2. Finishing “Dear Friend” (s.91-96)

  • Spam score (s.91): , i.e. . “Like before, we can think of 0.01 as the score that Dear Friend gets if it is Spam” (s.91).
  • Technically only proportional (s.92): the product is proportional to (symbol ) the probability that the message is spam given that it says “Dear Friend”: . It is a score, not a true posterior (the normalising constant is dropped; the same constant would divide both classes).
  • Normal score (s.93): earlier result (= ).
  • Comparison and decision (s.94-95): the Normal score 0.09 is greater than the Spam score 0.01, so “we will decide that Dear Friend is a Normal Message” (s.95; the slide shows the “Dear Friend” envelope turning blue).
  • Recap of the method (s.96): “Now that we understand the basics of how Naive Bayes Classification works…”:
    • Pick the class with the larger score.

3. A more complicated example: “Lunch Money Money Money Money” (s.97-103)

  • s.97: transition slide: “…let’s look at a slightly more complicated example.”
  • s.98: new message to classify: Lunch Money Money Money Money.
  • s.99: NOTE: the message contains the word Money four times. Word counts matter (repeated words are multiplied once per occurrence).
  • s.100: intuition: versus . The slide text says “(0.56)” for spam but the formula box shows 0.57 (slide inconsistency, probably rounding; 0.57 is used in the calculations). Money is much more probable in spam than in normal messages.
  • s.101: “…then it seems reasonable to predict that this message will end up being spam.” (s.102: “So let’s do the math!“)

4. Normal-message score for the long message (s.103-107)

Built up step by step (each step multiplies one more term):

  1. Start with the prior guess (= 0.67) (s.103).
  2. Multiply by the probability of seeing Lunch in a normal message: (0.18) (s.104).
  3. Multiply by the probability of seeing Money four times, given normal: (s.105). The exponent 4 is the word count.
  4. Result: (“this tiny number”) (s.106).

5. Spam score for the long message: it is 0 (s.107-114)

  • Same calculation for spam: (s.107).
  • s.108: “…we get 0.”
  • s.109: Reason: the probability of seeing Lunch in spam is 0, “since it was not in the Training Data” (the red box highlights ).
  • s.110-111: plugging in 0 gives .
  • s.112: it doesn’t matter what value we picked for the initial guess (0.33 highlighted)…
  • s.113: …and it doesn’t matter what the probability of Money given spam is (0.57 highlighted)…
  • s.114: “…because anything times 0…” (red box around the ) and s.115: “…is 0.”

6. Why this is a problem (s.116-118)

  • s.116: “In other words, if a message contains the word Lunch, it will not be classified as spam” (arrow from the word Lunch to the zero spam score).
  • s.117-118 (two identical-looking slides, an animation duplicate): “…and that means we will always classify the messages with Lunch in them as normal, no matter how many times we see the word Money.” The message envelope turns blue; comparison shown: normal vs spam .
  • s.119: “And that’s a problem.” (same two scores still on screen).

7. The fix: add one count to each word (s.120)

  • s.120: “To work around this problem, people usually add 1 count, represented by a black box, to each word in the histograms.”
  • The spam histogram (counts as red boxes) becomes: Dear (2 red + 1 black), Friend (1 red + 1 black), Lunch (0 red + 1 black), Money (4 red + 1 black). The normal histogram (greyed out) also gets a grey box on top of each bar (Dear, Friend, Lunch, Money).
  • Effect: Lunch now has a non-zero count in spam, so and the product is no longer wiped out. (The recomputed probabilities are on the following slides, i.e. the next chunk.) See Concept - Laplace Smoothing.

Added context

The black-box fix is called Laplace / add-one / additive smoothing, with the added pseudocount usually called (here ). With it, where is the vocabulary size. The slides in this chunk only show the “add 1 count to each word” step, not this formula; the arithmetic follows in the next chunk. Another practical point not on these slides: multiplying many small probabilities underflows (Concept - Underflow), so implementations sum log-probabilities instead.

Figures & diagrams

  • s.91-96 (every slide): left panel with two stacks of envelopes. Blue stack = normal messages () with word probabilities Dear 0.47, Friend 0.29, Lunch 0.18, Money 0.06; red stack = spam () with Dear 0.29, Friend 0.14, Lunch 0.00, Money 0.57. Faded rows show what is not currently used. Takeaway: class-conditional word probabilities plus the prior are all Naive Bayes needs.
  • s.91-95: Spam formula with red box around 0.01 and arrows comparing 0.09 vs 0.01; the “Dear Friend” label turns blue/envelope appears blue when decided as normal (s.95).
  • s.96: the two scoring formulas side by side ( and ).
  • s.98-102: message banner “Lunch Money Money Money Money” and arrows from and : shows the intuitive prediction (spam).
  • s.103-106: incremental construction of the normal-class product ending in 0.000002.
  • s.107-115: spam-class product ending in 0; red boxes highlight , the term and the result 0.
  • s.117-118: both scores displayed (0.000002 vs 0), with the blue “normal” envelope on the message.
  • s.120: four histograms (Dear, Friend, Lunch, Money) for normal (light blue/grey) and spam (red); a black box added on top of each bar; Lunch in spam consists only of the black box. Takeaway: smoothing adds a pseudocount so no word has zero probability.
flowchart TD
    M["Message: Lunch Money Money Money Money"] --> N["Normal score<br/>p(N) x p(Lunch|N) x p(Money|N)^4<br/>= 0.67 x 0.18 x 0.06^4 = 0.000002"]
    M --> S["Spam score<br/>p(S) x p(Lunch|S) x p(Money|S)^4<br/>p(Lunch|S) = 0 (never in spam training data)<br/>= 0"]
    N --> D{"Compare scores"}
    S --> D
    D --> R["Classified NORMAL<br/>(wrong intuition: looks like spam)"]
    R --> P["Problem: any unseen word in a class zeroes that class"]
    P --> F["Fix: add 1 count (pseudocount) to every word in every histogram"]
flowchart LR
    A["Dear Friend"] --> B["N: 0.67 x 0.47 x 0.29 = 0.09"]
    A --> C["S: 0.33 x 0.29 x 0.14 = 0.01"]
    B --> D{"0.09 > 0.01"}
    C --> D
    D --> E["Normal message"]

Key terms

Formulas

  • Class score for a message with words (s.96, s.103-107): , with repeated words counted repeatedly, e.g. (s.105, s.107).
  • Proportionality (s.92):
  • Decision rule (s.94-95): predict .
  • “Dear Friend”: (N) vs (S) (s.91, s.93, s.96).
  • “Lunch Money Money Money Money”: N: (s.106); S: (s.108-111).

Likely exam questions

  1. Explain how Naive Bayes classifies the message “Dear Friend” as normal or spam. (s.91-96) Compute and ; the larger score (normal) wins.
  2. Why is the score only proportional to ? (s.92) The product omits the normalising term (the probability of the message itself), which is identical for both classes so the ranking is unaffected.
  3. How are repeated words handled in the calculation? (s.99, s.105) Each occurrence multiplies in once, so Money four times gives .
  4. Discuss the problem that arises when classifying “Lunch Money Money Money Money”. (s.107-118) as Lunch never occurred in spam training data; the whole spam score becomes 0 regardless of the prior or of Money’s high probability, so the message is always classified as normal.
  5. Why does the prior not matter when the score is 0? (s.112-115) Anything multiplied by 0 is 0.
  6. Determine the correctness: “A message containing a word never seen in spam training data can still be classified as spam by (unsmoothed) Naive Bayes.” (s.116-118) False; the spam score is 0, so it is never classified as spam.
  7. What is the usual remedy for zero probabilities, as discussed in class? (s.120) Add 1 count (a pseudocount, the “black box”) to each word in each histogram (Laplace/additive smoothing).
  8. Which scores are compared in the “Lunch Money” example? (s.117-118) Normal 0.000002 vs spam 0.
  9. Why is the tiny number 0.000002 still enough to win? (s.106, s.118) Naive Bayes only compares scores between classes; the absolute magnitude is irrelevant, only that it is larger than the spam score of 0.
  10. Describe what the black boxes in the histograms represent. (s.120) One extra count added to every word’s bar in both the normal and spam histograms, so no word has probability 0.

Past-exam link: Naive Bayes / smoothing had no confirmed entry in the past exam; if it appears there, see Past Exam - Question Bank & Style Guide.

Common confusions

  • Score vs probability: the numbers 0.09 and 0.01 are scores, proportional to the posteriors, not probabilities that sum to 1 (s.92).
  • Intuition vs result: the message looks like spam (lots of Money) but Naive Bayes calls it normal because of the zero for Lunch in spam; the failure is of the estimation (zero count), not of the idea (s.101 vs s.118).
  • Zero probability is a training-data artefact: does not mean Lunch can never appear in spam, only that it was not observed (s.109).
  • Smoothing is applied to counts in every histogram, both classes, not just the zero one (s.120).
  • Do not confuse the prior with word probabilities; the prior reflects the proportion of messages per class (s.103, s.112).

Slide coverage

SlideContentCovered in
91Spam score for “Dear Friend”: 0.33 x 0.29 x 0.14 = 0.01Detailed notes 2
92The 0.01 is proportional to p(S given Dear Friend)Detailed notes 2
93Normal score 0.09 highlightedDetailed notes 2
940.09 greater than 0.01Detailed notes 2
95Decision: Dear Friend is a normal messageDetailed notes 2
96Recap of both formulas (0.09 vs 0.01)Detailed notes 2; Formulas
97Transition: slightly more complicated exampleDetailed notes 3
98New message: Lunch Money Money Money MoneyDetailed notes 3
99Note: Money occurs four timesDetailed notes 3
100p(Money given S) 0.57 much higher than p(Money given N) 0.06Detailed notes 3
101Reasonable to predict spamDetailed notes 3
102”So let’s do the math!”Detailed notes 3
103Start with prior p(N)Detailed notes 4
104Multiply by p(Lunch given N)Detailed notes 4
105Multiply by p(Money given N)^4Detailed notes 4
106Normal score = 0.000002Detailed notes 4
107Spam formula set upDetailed notes 5
108Spam score = 0Detailed notes 5
109Because p(Lunch given S) = 0 (not in training data)Detailed notes 5
110Plug in 0 for p(Lunch given S)Detailed notes 5
111Formula with 0 term = 0Detailed notes 5
112Initial guess p(S) does not matterDetailed notes 5
113p(Money given S) does not matterDetailed notes 5
114Anything times 0…Detailed notes 5
115…is 0Detailed notes 5
116Message with Lunch will not be classified as spamDetailed notes 6
117Always classified as normal regardless of Money countDetailed notes 6; Figures
118Same slide as 117 (animation duplicate)Detailed notes 6
119”And that’s a problem.”Detailed notes 6
120Fix: add 1 count (black box) to each word in histogramsDetailed notes 7; Figures

Previous: 03 Text Classification - Chunk 03 (s.61-90) | Next: 03 Text Classification - Chunk 05 (s.121-150) | Up: 03 Text Classification - Index