The Naive Bayes Classifier
You'll learn to
- -Apply Bayes' theorem from the Math module to build an actual classifier
- -Understand the "naive" conditional independence assumption and why it works anyway
- -Implement a Naive Bayes spam classifier from scratch
The Math module built up Bayes' theorem in detail, including the rare disease example. This chapter is the payoff. It turns that formula directly into a working classifier, one that remains a genuinely strong, fast baseline for text classification specifically, including the exact spam-filtering example this course has referenced since its first chapter.
From Bayes' Theorem to a Classifier
To classify an email as spam or not, Naive Bayes picks whichever class has the higher posterior probability given the email's words. It is directly applying Bayes' theorem, with "the disease" swapped for "the class" and "the test result" swapped for "the words in this email."
The "Naive" Part: Assuming Independence
Multiplying the individual word probabilities together is only mathematically valid if the words are conditionally independent given the class, meaning seeing "free" tells you nothing about the chance of also seeing "money," once you already know the email is spam. That assumption is obviously false in real language, since word choices are correlated. That is exactly why the algorithm is called naive. Remarkably, the classifier still works well in practice even though the assumption is wrong, which is a genuinely famous, often-tested piece of ML folklore.
Why does a classifier built on a false assumption still work? Because Naive Bayes only needs to get the ranking between classes right, not the exact probability value. Even with correlated words skewing the raw numbers, the skew is often similar enough across classes that the higher-scoring class stays correct.
Try it yourself below. Tap a few words and watch the classifier's confidence shift with every one.
Build Your Own Email
Tap words to add them to a message. Watch the classifier's verdict update after every word.
Predicted class: spam (log P(spam) = -4.10, log P(ham) = -6.88)
The "+1" in the code above is Laplace smoothing. Without it, any word never seen in a class during training would give that class a probability of exactly zero for the whole email, since the terms are multiplied together, even if every other word pointed strongly toward it. Adding 1 to every count guarantees no probability is ever exactly zero.
Why It Is Still a Strong Baseline
- -It is extremely fast to train: a single pass over the data counting words, with no iterative optimization needed.
- -It works well with relatively little training data compared to more complex models.
- -It naturally handles high-dimensional, sparse features, like whether an email contains word X across a 50,000-word vocabulary, that trip up some other classical methods.
- -It remains a genuinely common first baseline for text classification and spam filtering in production, not just a teaching exercise.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.