Sub topic 3
Classification as modeling P(x | y) and P(y)
This chapter assumes you know conditional probability and Bayes' theorem (covered in the previous chapters). It also assumes you know what features, labels, and a classifier are. The chapter shows how Bayes' theorem becomes a way to build a classifier.
Two ways to model a classifier
A classifier needs the posterior P(y | x): the probability of class y given features x. There are two ways to get it.
- A discriminative model learns P(y | x) directly. Logistic regression is an example.
- A generative model learns how the data is produced. It estimates P(y), the prior, and P(x | y), the likelihood. Bayes' theorem then turns these into P(y | x).
Naive Bayes is a generative model.
The generative story
A generative model describes a two-step process for creating one example. First, nature picks a class y with probability P(y). Second, given that class, it produces features x with probability P(x | y). Classification runs this process backwards. You observe x and ask which class most likely produced it.
Why P(x) can be ignored
Bayes' theorem gives:
P(y | x) = P(x | y) P(y) / P(x)
The denominator P(x) is the same for every class. To pick the best class you only compare classes, so you can drop it:
predicted class = argmax over y of P(x | y) P(y)
This shortcut matters. Estimating P(x) is hard, and you never need it for the prediction. You only need it if you want actual probabilities, which you get by dividing each score by the sum of all class scores.
A worked example
Suppose 40% of emails are spam. The word "free" appears in 50% of spam and in 5% of ham (non-spam). An email contains "free". Which class is more likely?
prior = {"spam": 0.4, "ham": 0.6}
likelihood_free = {"spam": 0.5, "ham": 0.05}
scores = {c: likelihood_free[c] * prior[c] for c in prior}
evidence = sum(scores.values())
posterior = {c: s / evidence for c, s in scores.items()}
print(scores)
print(posterior)
print(max(scores, key=scores.get))The unnormalized scores are 0.2 for spam and 0.03 for ham. Dividing by their sum, 0.23, gives a spam posterior of about 0.87. The prediction is "spam". The argmax would be the same without the division.
The catch with P(x | y)
With one binary feature, P(x | y) is a single number per class. With many features, x is a whole vector, and P(x | y) is a joint distribution over all of them. With 1,000 binary features, a full table has 2^1000 entries per class, and no dataset can fill it. The next chapter introduces the conditional independence assumption, which makes this estimation practical.
Common errors
- Comparing P(x | y) alone and forgetting the prior. A rare class can have a high likelihood and still be unlikely overall.
- Treating the normalized scores as trustworthy probabilities. They are only as good as the model of P(x | y).
- Confusing P(x | y) with P(y | x). A word that is common in spam does not make most emails containing it spam.
Exercise
Using the example above, suppose spam were only 5% of all email, with the same likelihoods for "free". Which class is predicted for an email containing "free"?
Show answer
The scores are spam 0.5 × 0.05 = 0.025 and ham 0.05 × 0.95 = 0.0475. Ham scores higher, so the prediction changes to ham. A low prior can outweigh a higher likelihood.