Naive Bayes Classifier

GATE Relevance: Very High

🎯 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:

P(X1, X2, …, Xn | C) = ∏i=1n P(Xi | C)

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

C* = argmaxc P(C = c) ∏i=1n P(Xi | C = c)

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

P(Xi = x | C = c) = (count(x, c) + 1) / (count(c) + |V|)

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

PropertyNaive BayesLogistic RegressionDecision Tree
Training speedVery fastFastModerate
Handles high-dim featuresExcellentGoodPoor
Feature dependenciesIgnoresLinearCaptures
Small data performanceGoodModerateOverfits
Calibrated probabilitiesPoorGoodPoor

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