ML Notes
Complete guide to Naive Bayes classification including Gaussian, Multinomial, and Bernoulli variants with text classification examples and probability calculations.
Naive Bayes is a probabilistic classifier based on Bayes' theorem with the "naive" assumption that features are independent given the class. Despite this simplifying assumption, it works remarkably well in practice, especially for text classification.
The Math Behind Naive Bayes
Bayes' Theorem
P(class|features) = P(features|class) × P(class) / P(features)
Naive assumption (features are independent)
P(x₁, x₂, ..., xₙ | class) = P(x₁|class) × P(x₂|class) × ... × P(xₙ|class)
Prediction: Choose class with highest posterior probability
ŷ = argmax P(class) × ∏ P(xᵢ|class)
Types of Naive Bayes
from sklearn.naive_bayes import GaussianNB, MultinomialNB, BernoulliNB
from sklearn.datasets import load_iris, fetch_20newsgroups
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
# 1. GaussianNB - for continuous features (assumes normal distribution)
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
gnb = GaussianNB()
gnb.fit(X_train, y_train)
print(f"GaussianNB on Iris: {gnb.score(X_test, y_test):.4f}")
# 2. MultinomialNB - for word counts/TF-IDF (text classification)
# 3. BernoulliNB - for binary features (word presence/absence)Text Classification (Spam Detection)
Gaussian Naive Bayes from Scratch
When to Use Naive Bayes
| Scenario | Good Choice? | Reason |
|---|---|---|
| Text/document classification | ✓ | Works great with word frequencies |
| Small training dataset | ✓ | Needs less data to estimate parameters |
| Real-time prediction | ✓ | Very fast inference |
| Features are correlated | ✗ | Violates independence assumption |
| Complex decision boundaries | ✗ | Assumes simple boundaries |
Interview Questions
- Why is it called naive?
Because it naively assumes all features are independent given the class. In reality, features are often correlated (e.g., word pairs in text), but the algorithm still works well empirically.
- Why does Naive Bayes work well for text classification despite the independence assumption?
Text classification cares about the presence of discriminative words, not their correlations. Even if "free" and "money" co-occur, knowing either alone helps classify spam.
- What is the zero-frequency problem and how do you solve it?
If a word never appears with a class in training, P(word|class)=0, making the entire product zero. Solution: Laplace smoothing (add 1 to all counts).
- Gaussian vs Multinomial vs Bernoulli — when to use each?
Gaussian: continuous features. Multinomial: word counts or TF-IDF. Bernoulli: binary features (word present/absent). Match the distribution to your data type.
- Can Naive Bayes output calibrated probabilities?
Not well — the independence assumption makes probabilities tend toward 0 or 1 (overconfident). Use CalibratedClassifierCV to fix probability estimates.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Naive Bayes Classifier.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, classification, naive, bayes, naive bayes classifier
Related Machine Learning Topics