Sub topic 6
Estimating priors and likelihoods from data
This chapter assumes you know the naive Bayes decision rule from the previous chapter, basic probability, and how a labeled training set is organized. Here you will learn how to turn counts into the numbers that rule needs.
What needs estimating
The decision rule scores each class c as P(c) × P(x₁ | c) × … × P(xₙ | c). Two kinds of parameters appear:
- Class priors P(c), one per class.
- Per-feature likelihoods P(xⱼ | c), one per feature and class.
Training a naive Bayes model means estimating both from labeled data. No iterative optimization is needed. Each estimate is a ratio of counts, which is the maximum likelihood estimate (MLE) under the model.
Estimating priors
The prior for class c is the fraction of training examples with that label:
P(c) = N_c / N
Here N_c is the number of examples in class c, and N is the total number of examples. In the spam example from the topic's code, two of four documents are spam, so P(spam) = 0.5.
Estimating likelihoods
The estimate depends on the feature type. For word-count features, the multinomial model uses:
P(w | c) = count(w, c) / total_words(c)
Here count(w, c) is how many times word w appears in all documents of class c. The denominator is the total number of word tokens in that class. For the spam class above, the documents are "free prize now" and "free offer now". That gives 6 tokens, and "free" appears twice, so P(free | spam) = 2/6 ≈ 0.33.
The same count-and-divide pattern applies to other variants:
| Variant | What is estimated per class and feature |
|---|---|
| Multinomial | Relative frequency of each word |
| Bernoulli | Fraction of documents in the class containing the feature |
| Gaussian | Mean and variance of the feature values |
Later chapters cover each variant in detail.
The estimation flow
A worked implementation
The train function from the topic's from-scratch example follows these steps. Here is a stripped-down version that shows the unsmoothed estimates only:
from collections import Counter, defaultdict
docs = [
["free", "prize", "now"],
["meeting", "at", "noon"],
["free", "offer", "now"],
["project", "meeting", "notes"],
]
labels = ["spam", "ham", "spam", "ham"]
class_counts = Counter(labels)
word_counts = defaultdict(Counter)
for d, c in zip(docs, labels):
word_counts[c].update(d)
priors = {c: n / len(docs) for c, n in class_counts.items()}
total_words = {c: sum(word_counts[c].values()) for c in class_counts}
likelihood_free_spam = word_counts["spam"]["free"] / total_words["spam"]
likelihood_free_ham = word_counts["ham"]["free"] / total_words["ham"]
print(priors)
print("P(free | spam) =", likelihood_free_spam)
print("P(free | ham) =", likelihood_free_ham)The output shows priors of 0.5 each, P(free | spam) = 0.333…, and P(free | ham) = 0.0.
A common error
The last value is the problem. "free" never appears in a ham document, so its estimated likelihood is exactly zero. Because the score multiplies likelihoods, any message containing "free" gets a ham score of zero, no matter what else it says. One unseen word vetoes the whole class. The next chapter fixes this with Laplace smoothing.
Exercise
A training set has 30 spam and 70 ham emails. The spam emails contain 600 word tokens in total, and "prize" appears 12 times in them. What are P(spam) and P(prize | spam) under the multinomial estimates?
Show answer
P(spam) = 30/100 = 0.3. P(prize | spam) = 12/600 = 0.02.