Quiz 2
Registry Synced

Naive Bayes Classifier

797 words
4 min read

Reading compass

Now · 🎯 Learning Objectives

Naive Bayes Classifier

🎯 Learning Objectives

  • Derive Naive Bayes from Bayes' theorem
  • Explain the "naive" conditional independence assumption
  • Implement Gaussian, Multinomial, and Bernoulli Naive Bayes
  • Apply Naive Bayes to text classification

📖 Core Content

2.1 Intuition: The Simple-but-Surprisingly-Effective Classifier

Imagine diagnosing a disease based on symptoms. A doctor might think: "Given the patient has a fever AND cough AND fatigue, what's the probability they have the flu?" Bayes' theorem says: look at how common the flu is (prior), and how common each symptom is given the flu (likelihood). The "naive" part: we assume symptoms are independent given the disease — even though we know they're not. Despite this strong assumption, Naive Bayes works surprisingly well, especially for text classification.

2.2 Bayes' Theorem

P(yx)=P(xy)P(y)P(x)P(y | x) = \frac{P(x | y) P(y)}{P(x)}
  • P(yx)P(y | x): posterior — probability of class y given features x
  • P(xy)P(x | y): likelihood — probability of features x given class y
  • P(y)P(y): prior — probability of class y (before seeing data)
  • P(x)P(x): evidence — probability of features x (marginal)

2.3 The Naive Assumption

P(x1,x2,,xny)=j=1nP(xjy)P(x_1, x_2, \dots, x_n | y) = \prod_{j=1}^{n} P(x_j | y)
This assumes features are conditionally independent given the class. In practice, this is almost always false (e.g., "Houston" and "Texans" are not independent in an email), but the classifier still works well because it only needs the argmax class, not calibrated probabilities. Decision rule:
y^=argmaxyP(y)j=1nP(xjy)\hat{y} = \arg\max_y P(y) \prod_{j=1}^{n} P(x_j | y)

2.4 Types of Naive Bayes

VariantFeature DistributionWhen to Use
Gaussian NB$P(x_jy) \sim \mathcal{N}(\mu_{jy}, \sigma_{jy}^2)$
Multinomial NB$P(x_jy) = \frac{N_{jy} + \alpha}{N_y + \alpha n}$
Bernoulli NB$P(x_jy) \sim \text{Bernoulli}(p_{jy})$

2.5 Text Classification Example

Classify emails as "Spam" or "Not Spam." Vocabulary: ["free", "money", "meeting", "lunch", "win"] Training data:
  • Spam: "free money", "win free money", "money money"
  • Not Spam: "meeting lunch", "free lunch meeting", "meeting" Step 1: Compute priors. P(Spam) = 3/6 = 0.5, P(Not Spam) = 3/6 = 0.5. Step 2: Compute likelihoods with Laplace smoothing (α=1\alpha = 1). P("free"Spam)=2+15+5=310=0.3P(\text{"free"} | \text{Spam}) = \frac{2 + 1}{5 + 5} = \frac{3}{10} = 0.3 (2 occurrences in Spam, 5 total words in Spam, 5 vocab size) P("meeting"Spam)=0+110=0.1P(\text{"meeting"} | \text{Spam}) = \frac{0 + 1}{10} = 0.1 P("free"Not Spam)=1+16+5=2110.182P(\text{"free"} | \text{Not Spam}) = \frac{1 + 1}{6 + 5} = \frac{2}{11} \approx 0.182 P("meeting"Not Spam)=3+111=4110.364P(\text{"meeting"} | \text{Not Spam}) = \frac{3 + 1}{11} = \frac{4}{11} \approx 0.364 Step 3: Classify "free meeting":
  • P(Spam)×P(freeSpam)×P(meetingSpam)=0.5×0.3×0.1=0.015P(\text{Spam}) \times P(\text{free}|\text{Spam}) \times P(\text{meeting}|\text{Spam}) = 0.5 \times 0.3 \times 0.1 = 0.015
  • P(Not Spam)×P(freeNot Spam)×P(meetingNot Spam)=0.5×0.182×0.364=0.033P(\text{Not Spam}) \times P(\text{free}|\text{Not Spam}) \times P(\text{meeting}|\text{Not Spam}) = 0.5 \times 0.182 \times 0.364 = 0.033 Prediction: Not Spam (0.033 > 0.015).

2.6 Implementation

python
# runnable
from sklearn.naive_bayes import GaussianNB, MultinomialNB
from sklearn.datasets import load_iris, fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Gaussian NB on Iris
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=42)
gnb = GaussianNB()
gnb.fit(X_train, y_train)
y_pred = gnb.predict(X_test)
print(f"Gaussian NB accuracy: {accuracy_score(y_test, y_pred):.3f}")
# Multinomial NB for text (using 20 newsgroups subset)
categories = ['alt.atheism', 'soc.religion.christian']
newsgroups = fetch_20newsgroups(subset='train', categories=categories, shuffle=True, random_state=42)
# Convert text to bag-of-words
vectorizer = CountVectorizer(stop_words='english', max_features=1000)
X_bow = vectorizer.fit_transform(newsgroups.data)
y = newsgroups.target
X_train, X_test, y_train, y_test = train_test_split(X_bow, y, test_size=0.3, random_state=42)
mnb = MultinomialNB(alpha=1.0)
mnb.fit(X_train, y_train)
y_pred = mnb.predict(X_test)
print(f"Multinomial NB accuracy: {accuracy_score(y_test, y_pred):.3f}")

📝 Practice Questions

Q1: Why is the "naive" assumption called naive?
Because it assumes conditional independence of features given the class — that all features are independent when we know the class. In reality, features are almost always correlated (e.g., "Houston" and "Texans" in spam emails). It's "naive" because it makes this strong assumption despite knowing it's usually false. Q2: What is Laplace smoothing and why is it needed?
Laplace smoothing adds a small constant (usually 1) to all count estimates to avoid zero probabilities. Without it, if a word never appeared in training for class "Spam," P(wordSpam)=0P(word|Spam) = 0, and the entire product becomes 0 regardless of other words. Smoothing prevents this. Q3: Why does Naive Bayes work well for text classification despite the independence assumption?
  1. For argmax (which class to choose), only the ranking of probabilities matters, not the exact values
  2. Even if feature dependencies exist, the relative ordering often remains correct
  3. The independence assumption reduces variance (fewer parameters to estimate) — important with limited text data
  4. For most text tasks, presence/absence of words is strongly informative despite correlations Q4: Compare Gaussian NB and Logistic Regression.
AspectGaussian NBLogistic Regression
TypeGenerativeDiscriminative
BoundaryQuadratic (if variance differs per class)Linear
ConvergenceFast (one pass)Iterative
Small dataBetter (stronger assumptions)Worse
Large dataWorse (assumptions limit it)Better (assumptions relaxed)
Calibrated probsNoYes
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.