Vocabulary features. P(spam)=0.4, P(ham)=0.6. Word likelihoods:
word
P(w∣spam)
P(w∣ham)
"free"
0.7
0.1
"win"
0.5
0.05
Message contains both "free" and "win". Score each class:
Spam:0.4×0.7×0.5=0.14Why this step? Prior × product of independent word likelihoods (naive assumption).
Ham:0.6×0.1×0.05=0.003
0.14>0.003⇒ predict spam. Normalized: P(spam∣x)=0.1430.14≈0.979.
Why normalize? To turn the raw scores into a probability we divide by their sum =P(x).
Why logs? Adds numerical stability and lets us compare sums. Class B's smaller variance + closer mean wins here → predict B. (The narrower Gaussian rewards being close to its mean.)
Imagine sorting fruit into "apple" or "orange". Once I tell you it's an orange, you can guess its color and its bumpy skin separately without them needing to consult each other — knowing it's an orange already explains why they go together. Naive Bayes pretends every clue is like that: once you know the answer-box, each clue speaks on its own. Then you just multiply how well each clue fits each box and pick the biggest.
Naive Bayes ka core idea bahut simple hai: hum class predict karna chahte hain given features x. Bayes rule se P(y∣x)∝P(x∣y)P(y). Problem yeh hai ki poora joint P(x1,…,xn∣y) estimate karna impossible hai — bahut saare features ke saare combinations ke liye data hi nahi hoga. Isliye hum ek "naive" (bhola) assumption maarte hain: agar class pata ho, to saare features ek dusre se independent ho jaate hain. Matlab features aapas mein gossip karte hain, lekin class label unhe chup kara deta hai.
Is assumption se magic hota hai: joint distribution ek simple product ban jaata hai — P(x∣y)=∏iP(xi∣y). Ab har feature ka sirf apna chhota 1-D distribution estimate karo. Final rule: y^=argmaxyP(y)∏iP(xi∣y). P(x) ko hata dete hain kyunki wo har class ke liye same hai, ranking nahi badalti. Aur multiply karne se numbers bahut chhote ho jaate hain, isliye log le lete hain — sum ban jaata hai aur underflow nahi hota.
Do important gotchas yaad rakho. Pehla: agar koi word spam mein kabhi dikha hi nahi, to uska probability 0 ho jaata hai, aur ek 0 poore product ko 0 kar deta hai — isliye Laplace smoothing (add-one) lagate hain. Doosra: assumption real data mein aksar galat hoti hai (features correlated hote hain), phir bhi Naive Bayes accha classify karta hai, kyunki hume sirf sahi argmax chahiye, exact probability nahi. Isiliye spam filters mein yeh aaj bhi kaam karta hai — fast, simple, aur surprisingly strong baseline.
Test yourself — SVM, Naive Bayes & Probabilistic Models