Before you readA message contains “free” and “prize”. If spam is rare, is that enough to call it spam?
Watch: Naive Bayes, Clearly Explained! · StatQuest with Josh Starmer
Logistic regression learns P(y∣x) directly. Naive Bayes learns the class priorP(y) (how common the class is) and how likely each feature is within each class. Bayes’ rule then turns those into a probability for the class given the features.
Suppose x1 records whether a message contains “free” and x2 whether it contains “prize”. Instead of estimating how often every combination occurs, assume the features are independent given the class:
P(x∣y)=j∏P(xj∣y).
That is the “naive” part. It says that among spam messages, seeing “free” does not change the chance of seeing “prize”. It makes no claim that the words are independent across all messages.
For each class, multiply the prior by the likelihood to get a score sy=P(y)P(x∣y). Predict the class with the larger score. To get a posterior probability, divide the score by the sum of the scores. For spam this is P(y=1∣x)=s1/(s0+s1).
The two words are more common in spam. Lower the prior: how rare does spam have to be before the same words no longer tip the decision towards spam?
With yes/no features, the likelihood factors can be estimated by counting within each class. A zero count would wipe out the whole product. Laplace smoothing adds one count to each possible outcome before computing the fractions. Smoothing helps with rare words, but not with duplicated evidence: two copies of the same clue can make the model too confident.
Recall
What does naive Bayes assume about the features?
Recall
Why can you compare the scores without first turning them into probabilities?
Exercise
The prior probability of spam is 0.2. Among spam messages, half contain “free”; among other messages, one in eight does. What is the posterior probability of spam given “free”?
Exercise
None of four spam messages contains “prize”. With Laplace smoothing, what probability do you assign to seeing “prize” in a spam message?