Naive Bayes for text classification
WHY do we need this?
WHAT is the problem? A document is a bag of words . We want the class that maximizes .
WHY not model directly? Because lives in a gigantic space: with a vocabulary of size , there are possible documents. We could never estimate a separate probability for each. We need structure — and Bayes + independence gives it cheaply.
Deriving the classifier from first principles
Step 1 — We only need to compare classes. Since does not depend on , Why this step? Dividing every candidate by the same constant never changes which one is largest, so we drop it.
Step 2 — Expand the likelihood. By the chain rule, This is exact but has the same explosion of parameters. We simplify with the naive assumption.
Step 3 — The naive (conditional independence) assumption. Given the class, each word is assumed independent of the others: Why this step? It reduces parameters per class to just per class — we only need one number per (word, class) pair. It's "naive" because word order/co-occurrence is ignored.
Estimating the parameters (Multinomial model)
HOW do we get ? By counting words in the training documents of class .
The zero-probability disaster. If a word never appeared with class in training, , and the whole product becomes — one unseen word vetoes the entire class.

Worked example 1 — Spam filter
Training (word tokens):
| Class | docs | words |
|---|---|---|
| spam | 2 | "free money now", "free free win" |
| ham | 2 | "meeting now please", "please review" |
Vocabulary free, money, now, win, meeting, please, review, .
- Counts: spam has 6 tokens (): free×3, money×1, now×1, win×1.
- ham has 5 tokens (): meeting×1, now×1, please×2, review×1.
- Priors: .
Classify "free now". Use Laplace , denominator spam , ham .
Scores (drop equal prior 0.5): Why this step? We compare ; spam wins → classified as spam. ✓ (the word "free" drove it, "now" was neutral).
Worked example 2 — Why the log form is safer
A 100-word document with each gives a product , which underflows to in floating point. Using logs: , a perfectly representable number. Why? Logs turn dangerous tiny products into safe sums.
Bernoulli vs Multinomial (know the difference)
Common mistakes (Steel-manned)
Active recall
Recall Test yourself (hide answers)
- Why can we drop ? → It's constant across classes; doesn't affect argmax.
- What is the naive assumption? → Words conditionally independent given the class.
- Why take logs? → Prevent underflow; sums instead of tiny products; argmax unchanged.
- What breaks without smoothing? → Zero probability from unseen word zeroes the product.
- Multinomial vs Bernoulli feature? → Counts vs binary presence.
Recall Feynman: explain to a 12-year-old
Imagine two toy boxes. Box A ("junk mail") is full of words like free, win, money. Box B ("friends") has meeting, please, review. Someone hands you a note. You look at each word and guess which box it probably came from, and you keep a running tally. The box that "fits" more of the words wins. It's "naive" because we pretend each word is picked separately, ignoring that some words like to travel together — but it still guesses the right box surprisingly often.
The 80/20 core (what actually matters)
- — the whole method.
- Laplace smoothing so no probability is zero.
- Work in log-space.
Master those three and you can implement NB text classification.
Naive Bayes classification rule
Why can we ignore in Bayes' rule for classification
What is the "naive" assumption
Why does NB work despite the false independence assumption
Laplace smoothing formula
Why add in the denominator
Why take logarithms of the probabilities
What zeroes out a class score without smoothing
Multinomial vs Bernoulli NB feature type
Prior estimate for class c
MLE for (unsmoothed)
Connections
- Bayes Theorem — the engine that flips likelihood into posterior.
- Maximum Likelihood Estimation — how counts become probabilities.
- Laplace Smoothing — handling unseen events / additive priors.
- Bag of Words Model — the document representation NB assumes.
- TF-IDF — alternative weighting often compared with raw counts.
- Logistic Regression — the discriminative counterpart to generative NB.
- SVM for text classification — margin-based alternative in the same chapter.
- Underflow and Log-space computation — numerical-stability trick reused everywhere.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Naive Bayes ka core idea simple hai: hume decide karna hai ki koi document (email, message) kis class ka hai — jaise spam ya ham. Hum Bayes theorem use karte hain jo "words given class" ko "class given words" me flip kar deta hai. Formula banta hai: har class ke liye us class ka prior le lo, aur us document ke saare words ki probabilities multiply kar do. Jis class ka score sabse bada, wahi answer.
"Naive" isliye bola jaata hai kyunki hum assume karte hain ki words ek dusre se independent hain given the class — matlab "New" aur "York" saath aate hain ye baat ignore kar dete hain. Ye assumption technically galat hai, par magic ye hai ki classification me sirf argmax matter karta hai, exact probability nahi. Isliye ranking sahi aa jaati hai aur accuracy achhi rehti hai.
Do practical cheezein yaad rakho. Pehli: Laplace smoothing — agar koi word training me kisi class ke saath kabhi nahi aaya, to uski probability 0 ho jaayegi aur poora product 0 ho jaayega, ek word puri class ko maar dega. Isliye upar aur neeche daalte hain. Dusri: log lena — bahut saari choti probabilities multiply karne se number itna chota ho jaata hai ki computer usko 0 samajh leta hai (underflow). Log lene se multiply, addition ban jaata hai, aur log monotonic hone ki wajah se argmax same rehta hai.
Yaad rakhne ka tarika: Prior × Likelihood, phir Log, phir Laplace. Bas yahi teen cheez pakad lo aur tum spam filter khud bana sakte ho — yahi 80/20 hai.