2.4.11 · HinglishSVM, Naive Bayes & Probabilistic Models

Naive Bayes for text classification

1,818 words8 min readRead in English

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.

Figure — Naive Bayes for text classification

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)

  1. — poora method.
  2. Laplace smoothing taaki koi probability zero na ho.
  3. 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
Yeh har class ke liye same hota hai, isliye argmax kabhi nahi badalta.
"Naive" assumption kya hai
Words class diye jane par conditionally independent hain: .
NB false independence assumption ke bawajood kyun kaam karta hai
Isko sirf chahiye ki sahi class ka score sabse bada ho; ranking biased probability estimates ke bawajood survive karti hai.
Laplace smoothing formula
Denominator mein kyun add karte hain
Humne vocabulary words mein se har ek mein add kiya, isliye kul added mass hai, jo distribution ko normalized rakhta hai.
Probabilities ke logarithms kyun lete hain
Kai tiny probabilities ka product 0 tak underflow kar jaata hai; logs unhe safe sum mein convert kar dete hain, aur log monotonic hai isliye argmax unchanged rehta hai.
Smoothing ke bina class score kya zero kar deta hai
Ek bhi word jo us class ke saath kabhi nahi dekha gaya, deta hai, jo poora product 0 bana deta hai.
Multinomial vs Bernoulli NB feature type
Multinomial word counts use karta hai; Bernoulli binary presence/absence use karta hai (aur absent words model karta hai).
Class c ke liye prior estimate
ke liye MLE (unsmoothed)
= class mein word count divided by class mein kul tokens.

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.

Concept Map

goal

flips likelihood

drop constant evidence

exact but explodes

reduces params to V per class

log for underflow

estimates

feeds

fixed by

corrects

estimates

feeds

Classify document x

Maximize P of c given x

Bayes theorem

argmax P of x given c times P of c

Chain rule expansion

Naive independence assumption

Product of P of wi given c times P of c

Sum of log probabilities

Multinomial counts

P of word given c

Zero-probability disaster

Laplace add-one smoothing

Doc frequency

Prior P of c