03 Text Classification - Chunk 06 (s.151-180)

Overview

This chunk finishes the worked spam/ham example that compares the two text variants of Naive Bayes: Concept - Multinomial Naive Bayes (MNB, uses word counts) and Concept - Bernoulli Naive Bayes (BNB, uses word presence/absence), both with Concept - Laplace Smoothing (s.151-160). It then shows how to turn the raw, unnormalized scores into proper posterior probabilities (Concept - Posterior Normalization, s.161). The rest of the chunk (s.162-180) starts Concept - Gaussian Naive Bayes (GNB): the assumptions (s.162) and a picture-based “Troll 2” example in which popcorn, soda pop and candy consumption are modelled with one Gaussian per feature per class (s.163-180). The slide numbers 163-180 are taken from a StatQuest-style video (logo top-left) and have almost no text, so the notes below describe each visual step.

Detailed notes

1. Test example: “cheap project meds” (s.151)

  • Test message: “cheap project meds”; count vector over the 9-word vocabulary: [0,1,1,0,0,0,0,1,0] (s.151).
    • Vocabulary order (from s.156): buy, cheap, meds, online, meeting, schedule, today, project, update. So cheap=1, meds=1, project=1.
  • In Multinomial NB (s.151): “cheap” and “meds” (strong spam indicators) occur once each; “project” (ham indicator) occurs once; prediction likely spam because the frequency of spammy words outweighs “project”.
  • In Bernoulli NB (s.151): the same words are marked present (1), but repetition would not matter if they occurred multiple times; the prediction “depends more evenly on presence of both spam and ham indicators” -> “could be more balanced”. (Note: the slide’s own numbers on s.156-160 end up giving BNB a stronger spam preference than MNB; see Common confusions.)

2. Multinomial NB worked through (s.152-155)

Step 1 - Training data recap (s.152). Four training documents:

ClassDocsWord countsTotal words
Spam1. “buy cheap meds”; 2. “cheap meds online”buy=1, cheap=2, meds=2, online=1; meeting, schedule, today, project, update = 06
Ham3. “meeting schedule today”; 4. “project schedule update”meeting=1, schedule=2, today=1, project=1, update=1; buy, cheap, meds, online = 06

Step 2 - Priors (s.153). , (class share of training documents).

Step 3 - Likelihoods (s.154). Use Concept - Laplace Smoothing with . Multinomial formula:

with (vocabulary size), so the denominator is .

Word
buy(1+1)/15 = 2/15(0+1)/15 = 1/15
cheap(2+1)/15 = 3/151/15
meds(2+1)/15 = 3/151/15
online(1+1)/15 = 2/151/15
meeting1/15(1+1)/15 = 2/15
schedule1/15(2+1)/15 = 3/15
today1/15(1+1)/15 = 2/15
project1/15(1+1)/15 = 2/15
update1/15(1+1)/15 = 2/15

(On the slide, spam lists buy, cheap, meds, online explicitly and “Others = (0+1)/15 = 1/15”; ham lists meeting, schedule, today, project, update explicitly and “Others = 1/15”. The table above just expands “Others”; the ham values for buy/cheap/meds/online are the slide’s “Others”.)

Step 4 - Classification (s.155).

  • Prediction = spam (0.00133 vs 0.000296).
flowchart LR
    A[Training docs<br/>2 spam, 2 ham] --> B[Step 1: word counts per class<br/>6 words each]
    B --> C[Step 2: priors 0.5 / 0.5]
    C --> D[Step 3: Laplace likelihoods<br/>count+1 over 6+9=15]
    D --> E[Step 4: multiply prior x P w given c<br/>for each word in test doc]
    E --> F{Compare scores}
    F -->|0.00133 > 0.000296| G[Predict spam]

3. Bernoulli NB worked through (s.156-159)

1) Vocabulary and binary features (s.156). Vocabulary buy, cheap, meds, online, meeting, schedule, today, project, update (size 9). BNB uses presence (1) / absence (0) per word. Test doc vector buy=0, cheap=1, meds=1, online=0, meeting=0, schedule=0, today=0, project=1, update=0.

2) Class document counts (s.156). (“buy cheap meds”, “cheap meds online”); (“meeting schedule today”, “project schedule update”).

3) Per-class word presence counts (s.156) = number of documents of class containing :

  • Spam (over 2 docs): buy=1, cheap=2, meds=2, online=1, meeting=0, schedule=0, today=0, project=0, update=0.
  • Ham (over 2 docs): buy=0, cheap=0, meds=0, online=0, meeting=1, schedule=2, today=1, project=1, update=1.

4) Laplace-smoothed Bernoulli likelihoods (s.157).

Word
buy2/4 = 0.50.25
cheap3/4 = 0.750.25
meds3/4 = 0.750.25
online0.50.25
meeting0.250.5
schedule0.250.75
today0.250.5
project0.250.5
update0.250.5

For absence use (s.157). Note the “+2” in the denominator (two outcomes: present/absent) versus "" in MNB.

5) Class-conditional likelihoods for the test doc (s.158). Bernoulli document likelihood:

  • Present words = {cheap, project, meds}:
    • Spam:
    • Ham:
  • Absent words = {buy, online, meeting, schedule, today, update}:
    • Spam:
    • Ham: the slide first writes and an intermediate value followed by a ”?”, then says “Careful - apply factors sequentially”: ; ; ; ; . The corrected value is 9/512 (the instructor’s self-correction on the slide).

Document likelihoods (s.159):

6) Unnormalized posterior scores, including priors (s.159). With equal priors 0.5:

  • Spam score
  • Ham score
  • “These are the raw (unnormalized) BNB scores; spam > ham => prediction = spam.”
flowchart TD
    V[Vocabulary of 9 words] --> X[Test vector x: cheap, meds, project = 1, others 0]
    X --> P1[Present words: multiply P w=1 given c]
    X --> P0[Absent words: multiply 1 - P w=1 given c]
    P1 --> L[P x given c = product over ALL 9 words]
    P0 --> L
    L --> S[Multiply by prior 0.5]
    S --> R[Spam 0.00555 vs Ham 0.000275 -> spam]

4. Bottom line: MNB vs BNB (s.160)

ModelSpam scoreHam scoreRatioInterpretation
Multinomial NB0.001330.000296spam about 4.5x more likely than ham
Bernoulli NB (full formula with absences)0.005550.000275spam about 20x more likely than ham

5. Normalizing raw scores into posteriors (s.161)

Question on slide: “How can we normalize the raw scores into proper posterior probabilities?” Normalization simply means

where the denominator is the sum over both classes (Concept - Posterior Normalization).

  • MNB: raw scores spam 0.00133, ham 0.000296, total 0.001626 -> , . MNB confidence: ~82% spam vs 18% ham.
  • BNB: raw scores spam 0.00555, ham 0.000275, total 0.005825 -> , . BNB confidence: ~95% spam vs 5% ham.
  • Difference: without normalization we only compare magnitudes (ratios ~4.5 vs ~20); with normalization we get actual posterior probabilities. Both models predict spam, but BNB is more confident (95% vs 82%).
  • Practical remark: normalization is what you use if you want interpretable probabilities (e.g. “spam with 95% certainty”), while unnormalized scores suffice for just choosing the most likely class.

6. Gaussian Naive Bayes - assumptions (s.162)

  • GNB assumes that features are continuous and follow a normal (Gaussian) distribution given the class.
  • Works well for sensor readings, continuous measurements, image pixel intensities.
  • If you use continuous embeddings such as word2vec, GloVe or BERT embeddings (discussed later; see Concept - Word Embeddings), GNB could be applied.

7. GNB intuition - the “Troll 2” example (s.163-180)

Title slide “Gaussian Naive Bayes…” (s.163). The slides (StatQuest “Double BAM!!!” logo, top-left) walk through building the class-conditional Gaussians:

  • Task (s.164): predict whether someone would love the 1990 movie Troll 2 or not. Two groups of people shown: “Loves Troll 2” (green, 8 figures) and “Does not Love Troll 2” (red, 8 figures).
  • Data (s.165-166): for the Loves group, a table with columns Popcorn (grams), Soda Pop (ml), Candy (grams): rows 24.3 / 750.7 / 0.2; 28.2 / 533.2 / 50.5; etc. For the Does-not-love group: 2.1 / 120.5 / 90.7; 4.8 / 110.9 / 102.3; etc.
  • Features (s.167-169): popcorn eaten each day, soda pop drunk, candy eaten (one slide highlights each column in both tables).
  • Popcorn, Loves Troll 2 (s.170-172): mean = 24, standard deviation = 4; a green Gaussian (bell curve) is drawn (footnote: “Gaussian Distributions are the same as Normal Distributions”).
  • Popcorn, Does not love (s.173-175): mean = 4, standard deviation = 2; a red bell curve, narrow and tall, located far left of the green curve (low popcorn consumption).
  • Soda pop (s.176-178): compute mean and standard deviation for people who love Troll 2 (green Gaussian centred high, wide), then the same for those who do not (red Gaussian centred lower, overlapping partially with green). The slides give no numeric mean/SD for soda pop (UNCLEAR whether values were stated; none on slides 176-178).
  • Candy (s.179-180): green Gaussian for loves: very narrow and tall near zero/low candy; red Gaussian for does-not-love: wide and flat, centred at high candy (about 90-100 g in the table). Both drawn on a third axis labelled Candy.
  • Takeaway of the build-up: for each class and each continuous feature, estimate a mean and standard deviation from the training data and draw the corresponding Gaussian; the class-conditional likelihood of a new value is read off these curves. (The chunk ends before the slides use these curves to classify a new person; that continues in the next chunk.)
flowchart LR
    D[Training data per class] --> M[For each feature: mean and std dev]
    M --> G[Gaussian curve per feature per class]
    G --> Q[New example: read each feature value on class curves]
    Q --> C[Combine with prior - next chunk]

Figures & diagrams

  • s.151 Text-only slide: test message, vector [0,1,1,0,0,0,0,1,0], MNB vs BNB commentary.
  • s.152 Two-column slide: spam docs and ham docs with per-word counts and totals (6 each).
  • s.153 Two bullets: priors 0.5 / 0.5.
  • s.154 Laplace-smoothed multinomial formula and per-word likelihood lists for spam (left) and ham (right).
  • s.155 MNB score computation for both classes ending in “Prediction = spam (0.00133 vs 0.000296)”.
  • s.156 BNB setup: vocabulary, test presence vector, class doc counts, per-class presence counts.
  • s.157 BNB Laplace formula and the two probability lists.
  • s.158 Bernoulli document likelihood formula; present/absent products, including the visible self-correction (“Careful - apply factors sequentially”).
  • s.159 Boxed final likelihoods , and unnormalized posteriors.
  • s.160 “Bottomline” ratio comparison (4.5x vs 20x).
  • s.161 Normalization formula and both models’ normalized posteriors; difference summary.
  • s.162 Text: GNB assumptions.
  • s.163 Title slide “Gaussian Naive Bayes…” with the StatQuest logo.
  • s.164 Green vs red people icons: loves vs does not love Troll 2.
  • s.165-166 Tables of popcorn/soda/candy for each group (green and red tables).
  • s.167-169 Column highlights: popcorn, soda pop, candy.
  • s.170-172 Popcorn mean/SD and green bell curve for Loves group.
  • s.173-175 Popcorn mean/SD and red bell curve for Does-not-love group; curves barely overlap.
  • s.176-178 Soda pop green and red Gaussians; overlapping.
  • s.179-180 Candy green (narrow) and red (wide) Gaussians; three stacked plots (Popcorn, Soda Pop, Candy) now complete.

Key terms

Formulas

  • Priors: ; example (s.153)
  • Multinomial likelihood with Laplace: , , denominator 15 (s.154)
  • MNB score: (s.155)
  • Bernoulli likelihood with Laplace: (s.157);
  • Bernoulli document likelihood: (s.158)
  • Normalization: (s.161)
  • Worked numbers: MNB 0.00133 vs 0.000296 (ratio 4.5; 0.818 vs 0.182); BNB 0.00555 vs 0.000275 (ratio 20.2; 0.953 vs 0.047) (s.155, 159-161)
  • Gaussian assumption (s.162): each continuous feature given class ~ (parameters = per-class mean and standard deviation, s.170-175).

Added context

The Gaussian density is the standard definition of the curve drawn on s.172-180; the slides in this chunk only show the curves, not the formula. Likewise, the expanded per-word table in section 2 simply spells out the slide’s “Others” lines.

Likely exam questions

(Style per Past Exam - Question Bank & Style Guide: short “discuss / describe / explain / determine the correctness” answers.)

  1. Discuss the difference between Multinomial NB and Bernoulli NB. (s.151, 154-158)
Multinomial NBBernoulli NB
Featureword counts (frequency)word presence/absence (0/1)
Likelihood used for each word occurrence in the doc for present and for absent words, over the whole vocabulary
Laplace smoothing ( = docs in class)
Repetitionmattersdoes not matter
Absent wordsignoredexplicitly penalized
  1. Classify “cheap project meds” with MNB, as discussed in class. (s.152-155) Priors 0.5/0.5; likelihoods with , denominator 15; spam ; ham ; predict spam.
  2. Why do we use Laplace smoothing and what is the denominator in MNB? (s.154) Without it an unseen word gives probability 0 and wipes out the whole product; add to every count; denominator = total words in class + = 6 + 9 = 15.
  3. What are the BNB likelihoods for cheap and meeting? (s.157) cheap = (2+1)/4 = 0.75; meeting = (0+1)/4 = 0.25.
  4. Explain the Bernoulli document likelihood formula. (s.158) Product over all vocabulary words: if present, if absent.
  5. How can raw NB scores be turned into probabilities? Give the result for MNB and BNB. (s.161) Divide each class score by the sum of the scores over all classes: MNB 0.818 / 0.182, BNB 0.953 / 0.047.
  6. Determine the correctness: “MNB and BNB always give the same confidence.” False; both predict spam but BNB gives ~95% vs ~82% for MNB (s.160-161).
  7. When is normalization needed? (s.161) Only when interpretable probabilities are required; to choose the most likely class the raw scores suffice.
  8. Describe the assumptions of Gaussian Naive Bayes and when it is used. (s.162) Continuous features, normal distribution of each feature given the class; sensor readings, measurements, pixel intensities, continuous embeddings (word2vec, GloVe, BERT).
  9. In the Troll 2 example, what parameters are estimated for each feature? (s.170-175) The mean and standard deviation per feature per class (e.g. popcorn: loves = mean 24, SD 4; does not love = mean 4, SD 2), giving a Gaussian per feature per class.

Common confusions

  • Count vs presence: MNB counts repeated words; BNB only records 0/1, but BNB also uses absent words in the product (s.151, 158).
  • Different smoothing denominators: MNB uses total words in class + (15); BNB uses class document count + 2 (4) (s.154, 157).
  • counts documents, not word occurrences (s.156), unlike MNB’s (s.152).
  • Slide 151 vs slides 156-160: s.151 says BNB “could be more balanced”, yet the computed BNB result is more confident in spam (95% vs 82%). Trust the computed numbers (s.160-161).
  • Slide self-correction: the ham absent-word product on s.158 is first written as 9/256 and then corrected to 9/512 = 0.017578125 - use 9/512 (the final likelihoods on s.159 use 9/512).
  • Raw scores are not probabilities: they do not sum to 1 until normalized (s.159, 161).
  • GNB is not for raw word counts: it needs continuous features (e.g. embeddings), unlike MNB/BNB (s.162).

Slide coverage

SlideContentCovered in
151Test example “cheap project meds”, count vector, MNB vs BNB commentsDetailed notes 1
152MNB step 1: training data recap, word countsDetailed notes 2
153MNB step 2: priorsDetailed notes 2
154MNB step 3: Laplace likelihoodsDetailed notes 2; Formulas
155MNB step 4: classification -> spamDetailed notes 2
156BNB: vocabulary, binary features, class doc counts, presence countsDetailed notes 3
157BNB: Laplace-smoothed Bernoulli likelihoodsDetailed notes 3; Formulas
158BNB: class-conditional likelihoods (present/absent words)Detailed notes 3; Formulas
159BNB: document likelihoods and unnormalized posteriorsDetailed notes 3
160Bottom line: MNB vs BNB ratiosDetailed notes 4
161Normalizing to posterior probabilitiesDetailed notes 5
162GNB assumptionsDetailed notes 6
163Title: “Gaussian Naive Bayes…”Detailed notes 7
164Troll 2 task: loves vs does not loveDetailed notes 7
165Data table for people who love Troll 2Detailed notes 7
166Data table for people who do not love Troll 2Detailed notes 7
167Feature: popcornDetailed notes 7
168Feature: soda popDetailed notes 7
169Feature: candyDetailed notes 7
170Popcorn mean (loves) = 24Detailed notes 7
171Popcorn standard deviation (loves) = 4Detailed notes 7
172Popcorn Gaussian for loves (mean 24, SD 4)Detailed notes 7; Figures
173Popcorn mean (does not love) = 4Detailed notes 7
174Popcorn standard deviation (does not love) = 2Detailed notes 7
175Popcorn Gaussian for does not loveDetailed notes 7; Figures
176Soda pop mean/SD for lovesDetailed notes 7
177Soda pop Gaussian for lovesDetailed notes 7; Figures
178Soda pop Gaussian for does not loveDetailed notes 7; Figures
179Candy Gaussian for lovesDetailed notes 7; Figures
180Candy Gaussian for does not loveDetailed notes 7; Figures

Previous: 03 Text Classification - Chunk 05 (s.121-150) | Next: 03 Text Classification - Chunk 07 (s.181-210) | Up: 03 Text Classification - Index