Skip to main content

Naive Bayes

Naive Bayes Probability Flow

The Probability Perspective

Most algorithms we’ve seen ask: “Which side of the boundary is this point on?” Naive Bayes asks: “Given the evidence, what’s the probability of each class?”

The Doctor’s Diagnosis Problem

A patient walks in with symptoms:
  • Fever: Yes
  • Cough: Yes
  • Fatigue: Yes
The doctor thinks: “Based on these symptoms, how likely is it they have the flu vs a cold?” This is Bayesian reasoning - updating beliefs based on evidence.
Spam Detection with Naive Bayes

Bayes’ Theorem

P(DiseaseSymptoms)=P(SymptomsDisease)×P(Disease)P(Symptoms)P(Disease|Symptoms) = \frac{P(Symptoms|Disease) \times P(Disease)}{P(Symptoms)} In English:
  • P(Disease|Symptoms): Probability of disease given symptoms (what we want)
  • P(Symptoms|Disease): How likely these symptoms are if you have the disease
  • P(Disease): How common the disease is (prior probability)
  • P(Symptoms): How common these symptoms are overall
Math Connection: This is Bayes’ Theorem from probability theory. See Probability for the full derivation.

Why “Naive”?

The “naive” assumption: All features are independent given the class. For our flu example:
  • P(Fever AND Cough AND Fatigue | Flu)
  • Approximately equals P(Fever|Flu) x P(Cough|Flu) x P(Fatigue|Flu)
Is this realistic? No! Symptoms often correlate — fever and fatigue almost always appear together. If we were computing the true joint probability, we’d need to account for all these correlations. Does it work anyway? Surprisingly well, yes! Here’s why: Naive Bayes only needs to get the ranking of class probabilities right, not the exact values. Even if the absolute probabilities are wildly off (e.g., 99.9% instead of 70%), as long as the winning class is correct, the classification is correct. The independence assumption distorts magnitudes but often preserves the ordering. It’s like a biased thermometer that always reads 10 degrees too high — useless for absolute temperature, but perfectly fine for telling you which room is hottest.

Building Naive Bayes From Scratch


Types of Naive Bayes

1. Gaussian Naive Bayes

For continuous features (assumes normal distribution):

2. Multinomial Naive Bayes

For count data (word frequencies, document classification):

3. Bernoulli Naive Bayes

For binary features (word presence/absence):

Real Example: Spam Classification


When Naive Bayes Shines

1. Text Classification

2. Fast Baseline Model


Laplace Smoothing

What if a word never appeared in training for a class?
Then the entire product becomes 0, regardless of other evidence! Solution: Add a small count to everything (Laplace/additive smoothing). Think of it as giving every word a “benefit of the doubt” — we pretend we’ve seen each word at least once in each class, even if we haven’t. This prevents any single unseen word from vetoing the entire classification.

Naive Bayes vs Other Algorithms


Probability Calibration

Naive Bayes probabilities are often overconfident. Because the independence assumption is wrong, the model tends to push probabilities toward 0 and 1. It might say “99.8% spam” when the true probability is 75%. This doesn’t hurt classification accuracy (it still picks the right class), but it’s a problem if you need reliable probability estimates — for example, when ranking items by risk or making decisions with different cost thresholds.

Key Takeaways

Probability-Based

Predicts class probabilities using Bayes’ theorem

Independence Assumption

Assumes features are independent (often wrong, still works!)

Fast & Simple

Trains instantly, great for baselines

Text Champion

Excels at document classification and spam filtering

What’s Next?

Now let’s learn about ensemble methods - combining multiple models for better predictions!

Continue to Module 6: Ensemble Methods

The wisdom of crowds - Random Forests and Gradient Boosting