Naive Bayes Classifier
Prerequisites
🎯 Learning Objectives
- State the Naive Bayes conditional independence assumption
- Derive and apply the Naive Bayes classification formula
- Work through a text classification example (spam detection)
- Apply Laplace smoothing to handle zero-frequency problems
- Compare Naive Bayes with other classifiers
1. The Naive Bayes Assumption
Conditional Independence Assumption
Given the class label C, all features X1, X2, …, Xn are conditionally independent:
This assumption is "naive" because features are rarely truly independent given the class, yet the classifier performs surprisingly well in practice.
As a Bayesian Network
Naive Bayes corresponds to a BN where the class node C is the single parent of all feature nodes. This star-shaped structure directly encodes the conditional independence assumption.
2. Classification Formula
Naive Bayes Classifier
Choose the class that maximizes the posterior. The denominator P(X1, …, Xn) is constant across classes and can be omitted for classification.
Example 1 — Play Tennis GATE Style
Features: Outlook=Sunny, Temperature=Hot, Humidity=High, Wind=Weak. Classes: Yes, No.
From training data: P(Yes) = 9/14, P(No) = 5/14
P(Sunny|Yes) = 2/9, P(Hot|Yes) = 2/9, P(High|Yes) = 3/9, P(Weak|Yes) = 6/9
P(Sunny|No) = 3/5, P(Hot|No) = 2/5, P(High|No) = 4/5, P(Weak|No) = 2/5
Score(Yes) = (9/14)(2/9)(2/9)(3/9)(6/9) = 9/14 × 0.0053 = 0.0034
Score(No) = (5/14)(3/5)(2/5)(4/5)(2/5) = 5/14 × 0.0384 = 0.0137
Since Score(No) > Score(Yes), predict No.
3. Text Classification: Spam Detection
Multinomial Naive Bayes for Spam GATE Style
Training set: 100 spam emails (contain 2000 total words), 200 ham emails (contain 5000 total words). Vocabulary size = 10000.
Word "free" appears 80 times in spam, 5 times in ham.
P(spam) = 100/300 = 1/3, P(ham) = 200/300 = 2/3
P("free"|spam) = 80/2000 = 0.04
P("free"|ham) = 5/5000 = 0.001
For an email containing just the word "free":
Score(spam) = (1/3)(0.04) = 0.0133
Score(ham) = (2/3)(0.001) = 0.000667
Classify as spam (after normalizing: P(spam) ≈ 95.2%)
4. Laplace Smoothing
The Zero-Frequency Problem
If a word never appears in spam training data, P(word|spam) = 0, making the entire product zero regardless of other features.
Laplace (Add-1) Smoothing
Where |V| is the vocabulary size (number of possible feature values). This ensures no probability is ever zero.
Example with Smoothing
Word "discount" appears 0 times in ham (5000 total words, |V| = 10000):
Without smoothing: P("discount"|ham) = 0/5000 = 0
With Laplace: P("discount"|ham) = (0 + 1)/(5000 + 10000) = 1/15000 ≈ 0.0000667
5. Comparison with Other Classifiers
| Property | Naive Bayes | Logistic Regression | Decision Tree |
|---|---|---|---|
| Training speed | Very fast | Fast | Moderate |
| Handles high-dim features | Excellent | Good | Poor |
| Feature dependencies | Ignores | Linear | Captures |
| Small data performance | Good | Moderate | Overfits |
| Calibrated probabilities | Poor | Good | Poor |
Summary
- Naive Bayes assumes features are conditionally independent given class
- Classification: pick class maximizing P(C) ∏ P(Xi|C)
- Laplace smoothing prevents zero probabilities
- Especially effective for text classification and high-dimensional data
- Frequently tested in GATE — know the formula and be able to compute by hand