How does Naive Bayes classify an email as spam? Work through a small example.
It uses Bayes' theorem: posterior is proportional to prior times the product of each word's likelihood given the class. Compute that score for spam and for not-spam, then normalise. The class with the higher posterior wins.
Explain it simply
A mail clerk notices that "free" and "offer" show up often in junk mail and rarely in genuine mail. When a new email contains both words, the clerk's suspicion rises. Naive Bayes turns that habit into arithmetic.
Example
Known from training data:
P(spam) = 0.4 P(ham) = 0.6
P(offer | spam) = 0.5 P(offer | ham) = 0.05
P(free | spam) = 0.4 P(free | ham) = 0.10New email contains "offer" and "free".
score(spam) = 0.4 x 0.5 x 0.4 = 0.08
score(ham) = 0.6 x 0.05 x 0.10 = 0.003
P(spam | words) = 0.08 / (0.08 + 0.003) = 0.08 / 0.083 = 0.964So about 96.4% spam.
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
model = make_pipeline(CountVectorizer(), MultinomialNB())In practice, implementations add log probabilities instead of multiplying, to avoid numbers so small that the computer rounds them to zero.
What to say in the interview
"Naive Bayes applies Bayes' theorem with the assumption that features are independent given the class. It multiplies the class prior by each feature's likelihood, compares the classes and normalises. It is fast, works with little data, and is a strong baseline for text."
Common mistakes
- Forgetting the prior.
- Multiplying many tiny probabilities instead of summing logs.