Naive Bayes for text classification
2.4.11· AI-ML › SVM, Naive Bayes & Probabilistic Models
WHY hamen yeh chahiye?
WHAT hai problem? Ek document words ka ek bag hai . Hamen woh class chahiye jo ko maximize kare.
WHY directly model nahi karte? Kyunki ek bahut bade space mein rehta hai: size ke vocabulary ke saath, possible documents hote hain. Hum har ek ke liye alag probability kabhi estimate nahi kar sakte. Hamen structure chahiye — aur Bayes + independence yeh sasta deta hai.
Classifier ko first principles se derive karna
Step 1 — Hamen sirf classes compare karni hain. Kyunki par depend nahi karta, Yeh step kyun? Har candidate ko same constant se divide karne par kabhi nahi badalta ki kaun sabse bada hai, isliye hum isse drop kar dete hain.
Step 2 — Likelihood expand karo. Chain rule se, Yeh exact hai lekin parameters ka wahi explosion hai. Hum naive assumption se simplify karte hain.
Step 3 — Naive (conditional independence) assumption. Class diye jane par, har word baaki se independent maana jaata hai: Yeh step kyun? Yeh parameters per class ko sirf per class tak reduce kar deta hai — hamen sirf ek number chahiye har (word, class) pair ke liye. Yeh "naive" hai kyunki word order/co-occurrence ignore hota hai.
Parameters estimate karna (Multinomial model)
HOW hamen milta hai? Class ke training documents mein words count karke.
Zero-probability disaster. Agar ek word training mein class ke saath kabhi nahi aaya, , aur poora product ban jaata hai — ek unseen word poori class ko veto kar deta hai.

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 mein 6 tokens hain (): free×3, money×1, now×1, win×1.
- ham mein 5 tokens hain (): meeting×1, now×1, please×2, review×1.
- Priors: .
"free now" classify karo. Laplace use karo, denominator spam , ham .
Scores (equal prior 0.5 drop karo): Yeh step kyun? Hum compare karte hain; spam jeet ta hai → spam classify kiya gaya. ✓ (word "free" ne drive kiya, "now" neutral tha).
Worked example 2 — Log form kyun safe hai
Ek 100-word document jisme har hai, product deta hai, jo floating point mein underflow kar jaata hai. Logs use karke: , ek perfectly representable number. Kyun? Logs khatarnak chhote products ko safe sums mein convert kar dete hain.
Bernoulli vs Multinomial (antar jaano)
Common mistakes (Steel-manned)
Active recall
Recall Khud test karo (answers hide karo)
- kyun drop kar sakte hain? → Yeh sabhi classes mein constant hai; argmax affect nahi karta.
- Naive assumption kya hai? → Words class diye jane par conditionally independent hain.
- Logs kyun lete hain? → Underflow rokne ke liye; tiny products ki jagah safe sums; argmax unchanged.
- Smoothing ke bina kya toot ta hai? → Unseen word se zero probability poore product ko 0 kar deti hai.
- Multinomial vs Bernoulli feature? → Counts vs binary presence.
Recall Feynman: ek 12-saal ke bacche ko samjhao
Socho do toy boxes hain. Box A ("junk mail") free, win, money jaise words se bhari hai. Box B ("friends") mein meeting, please, review hain. Koi tumhe ek note deta hai. Tum har word dekhte ho aur guess karte ho ki woh probably kis box se aaya, aur ek running tally rakhte ho. Jo box zyada words "fit" karta hai woh jeetta hai. Yeh "naive" hai kyunki hum maan lete hain ki har word alag se choose hota hai, yeh ignore karke ki kuch words saath chalte hain — lekin phir bhi yeh surprisingly aksar sahi box guess kar leta hai.
The 80/20 core (jo actually matter karta hai)
- — poora method.
- Laplace smoothing taaki koi probability zero na ho.
- Log-space mein kaam karo.
Yeh teen master karo aur tum NB text classification implement kar sakte ho.
Naive Bayes classification rule
Bayes' rule mein classification ke liye kyun ignore kar sakte hain
"Naive" assumption kya hai
NB false independence assumption ke bawajood kyun kaam karta hai
Laplace smoothing formula
Denominator mein kyun add karte hain
Probabilities ke logarithms kyun lete hain
Smoothing ke bina class score kya zero kar deta hai
Multinomial vs Bernoulli NB feature type
Class c ke liye prior estimate
ke liye MLE (unsmoothed)
Connections
- Bayes Theorem — woh engine jo likelihood ko posterior mein flip karta hai.
- Maximum Likelihood Estimation — counts probabilities kaise bante hain.
- Laplace Smoothing — unseen events / additive priors handle karna.
- Bag of Words Model — document representation jo NB assume karta hai.
- TF-IDF — alternative weighting jo raw counts se aksar compare hoti hai.
- Logistic Regression — generative NB ka discriminative counterpart.
- SVM for text classification — same chapter mein margin-based alternative.
- Underflow and Log-space computation — numerical-stability trick jo har jagah reuse hoti hai.