On this page
A naive Bayes classifier needs nothing beyond Class 12 probability. It labels an email spam or not spam by asking which class makes the email's words most likely. Below, we build one from ten emails, break it, fix it, and see why it works.
What is a naive Bayes classifier?
Start with Bayes' theorem: P(class | email) = P(class) × P(email | class) ÷ P(email). The denominator is the same for every class, so you can drop it and compare only the top line, as Domingos and Pazzani explain in Machine Learning (1997).
The naive step treats each word as independent of the others once the class is known, so P(email | class) becomes a product of one probability per word. The scikit-learn user guide writes the rule as: choose the class y that maximises P(y) × P(x1 | y) × P(x2 | y) × ... × P(xn | y).
If Bayes' theorem feels shaky, first read conditional probability and the prosecutor's fallacy and Bayes questions for the Class 12 board exam.
How does a naive Bayes spam filter work? A worked example
Suppose you have 10 training emails: 4 spam and 6 not spam (call it ham). So P(spam) = 0.4 and P(ham) = 0.6. You count how often four words appear in each pile:
free: 6 times in spam, 1 time in ham
win: 4 in spam, 0 in ham
meeting: 0 in spam, 6 in ham
report: 2 in spam, 5 in ham
Each pile has 12 word occurrences in total. A new email arrives: "free win meeting".
Without smoothing, P(win | ham) = 0/12 and P(meeting | spam) = 0/12. Both scores contain a zero factor, so both are 0 and the filter cannot decide.
What is Laplace smoothing in naive Bayes?
Laplace smoothing adds 1 to every count before dividing. The scikit-learn guide gives the estimate as (count + α) ÷ (total + α × n), where n is the vocabulary size, and states that α = 1 is called Laplace smoothing (scikit-learn).
Here n = 4, so every denominator becomes 12 + 4 = 16:
spam: free 7/16, win 5/16, meeting 1/16, report 3/16
ham: free 2/16, win 1/16, meeting 7/16, report 6/16
Now score "free win meeting":
Spam: 0.4 × 7/16 × 5/16 × 1/16 = 14/4096, about 0.00342.
Ham: 0.6 × 2/16 × 1/16 × 7/16 = 8.4/4096, about 0.00205.
Spam wins: "meeting" pulls towards ham, but "free" and "win" pull harder. Normalising, P(spam | email) = 14 ÷ (14 + 8.4) = 0.625.
Why work in log space?
Real emails have hundreds of words. Multiply 300 probabilities of about 0.01 and you get 10 to the power −600, too small for ordinary floating-point numbers, so the computer rounds it to zero. Add logarithms instead. Because ln is increasing, the class with the larger sum is the class with the larger product.
Using natural logs on our example:
Spam: ln 0.4 + ln(7/16) + ln(5/16) + ln(1/16) = −0.916 − 0.827 − 1.163 − 2.773 = −5.679
Ham: ln 0.6 + ln(2/16) + ln(1/16) + ln(7/16) = −0.511 − 2.079 − 2.773 − 0.827 = −6.190
Spam is higher by 0.511, and e to the power 0.511 is about 1.667, the ratio 14 ÷ 8.4. Same decision, no underflow. If logs need a refresher, the Fermi Concept Library has a lesson for every concept in mathematics and computing.
Why does naive Bayes work so well if the assumption is wrong?
Words are clearly not independent: "free" and "win" travel together. Yet the scikit-learn guide notes that naive Bayes classifiers "have worked quite well in many real-world situations, famously document classification and spam filtering", and need only a small amount of training data (scikit-learn).
The explanation is that a classifier only has to rank the classes, not get the probabilities right. Domingos and Pazzani showed that although the classifier's probability estimates are only optimal under quadratic loss when independence holds, the classifier itself can be optimal under zero-one loss (misclassification rate) even when the assumption is violated by a wide margin (Machine Learning, 1997). In our example, the 0.625 may be off while spam still comes out on top.
The same guide puts the catch plainly: naive Bayes is "a decent classifier" but "a bad estimator", so its probabilities should not be taken too seriously (scikit-learn). Hand and Yu's 2001 review, "Idiot's Bayes—Not So Stupid After All?", examines the empirical and theoretical evidence that this simple rule can be highly effective.
Try it yourself
Score "meeting report" with the smoothed table; ham should win clearly. For what a prior really means, read Bayesian vs frequentist statistics with one coin. If you get stuck on a step, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



