Naive Bayes ML Lecture
Naive Bayes
Review: Parameter Estimation with MLE and MAP
Machine learning models have parameters that act as sliders. Setting these sliders well is the core of parameter estimation. Two main philosophies exist:
Maximum Likelihood Estimation (MLE): Choose parameter values that make the observed training data as likely as possible. Mathematically, maximize logP(data∣parameters).
Maximum a Posteriori (MAP): Choose the most likely parameter values given the data, incorporating a prior belief about the parameters. Maximize logP(parameters∣data), which equals logP(data∣parameters)+logP(parameters) (up to a constant).
The key difference is the prior term in MAP. For a Bernoulli/binomial parameter p, using a Beta prior leads to a posterior distribution. Taking the mode of that posterior gives the MAP estimate.
MAP for Bernoulli with Beta Prior
For estimating the probability p of a Bernoulli trial:
Prior: p∼Beta(α,β), where α=imaginary successes+1, β=imaginary failures+1.
After observing a successes and b failures, the posterior is Beta(α+a,β+b).
MAP estimate (mode) is a+b+α+β−2a+α−1.
A common default prior is Laplace prior: Beta(2,2), which corresponds to one imaginary success and one imaginary failure. This gives the MAP estimate:
Why Laplace? It guarantees that the estimated probability is never exactly 0 or 1, avoiding division-by-zero issues and extreme predictions.
Classification Problem Formulation
A classification task involves predicting a label y (e.g., 0 or 1) from a set of input features represented as a vector x. Training data consists of n labeled examples: (x(1),y(1)),(x(2),y(2)),…,(x(n),y(n)).
x(i) is the feature vector for the i-th example, with m features: x(i)=(x1(i),x2(i),…,xm(i)).
y(i) is the true label (0 or 1) for that example.
Superscript indexes the example; subscript indexes the feature.
The goal: learn a classifier h(x) that predicts y from x.
Examples:
Movie recommendation: features are whether a user liked other movies, label is whether they like a target movie.
Medical diagnosis: features are abnormalities in heart region-of-interest images, label is healthy/unhealthy.
Ancestry prediction: features are nucleotide changes at specific locations, label is ancestry group.
All features and labels are binary (0/1) in this presentation — the core ideas extend to non-binary cases.
Brute Force Bayes
Idea: Predict the Most Probable Label Given Inputs
For a new input x, compute the probability of each possible label and choose the one with highest probability:
Using Bayes' theorem:
Since argmax over y ignores the denominator (it is constant across y), we have:
Single Feature (m=1)
With one input feature x (e.g., liked Independence Day), we need parameters:
P(y=1) and P(y=0)
P(x=1∣y=1), P(x=0∣y=1), P(x=1∣y=0), P(x=0∣y=0)
That is 6 parameters (though some are redundant). These are estimated by counting from training data:
P(y=1)≈ncount of examples with y=1
P(x=1∣y=1)≈count where y=1count where x=1 and y=1
Making a prediction: For a new x, compute P(x∣y=1)P(y=1) and P(x∣y=0)P(y=0), pick the larger.
Many Features: The Exponential Problem
For m binary features, the term P(x∣y) requires a joint probability over all feature combinations. For each y, there are 2m possible assignments to x, each needing its own parameter. Total parameters: 2×2m — exponential in m. For m=100, that's 2101≈2.5×1030 parameters — far more than atoms in the universe.
Brute force is infeasible for realistic feature counts.
The Naive Bayes Assumption
To make the problem tractable, we introduce a conditional independence assumption:
Given the class label y, the features x1,x2,…,xm are independent of each other.
This means:
This assumption is very wrong in most real-world scenarios (e.g., liking one movie is correlated with liking its sequel), but it drastically reduces the number of parameters:
For each feature xj, we need only P(xj=1∣y=1) and P(xj=1∣y=0) (or equivalently P(xj=0∣y) follows by complement).
Plus P(y=1) and P(y=0).
Total parameters: 2m+2 — linear in m.
This is the Naive Bayes classifier. It trades some accuracy for enormous computational savings, often performing surprisingly well in practice.
Training Naive Bayes: Estimating Parameters
Maximum Likelihood Estimation (MLE)
Parameters are simply counts from the training data.
Prior probability of class:
For k=0,1.
Conditional probabilities for each feature:
Similarly for P(xj=0∣y=k) (which is 1 minus the above).
Problem with MLE: If a particular combination never occurs in the training set (e.g., xj=1 never appears with y=1), the estimated probability becomes 0. This zero will propagate through the product in prediction, making the whole expression zero regardless of other features — a catastrophic failure.
Maximum a Posteriori (MAP) with Laplace Smoothing
To avoid zero probabilities, we use a Laplace prior (equivalent to Beta(2,2) for each Bernoulli parameter). This adds one imaginary success and one imaginary failure to each count.
For the class prior:
For each feature conditional:
The +1 in the numerator and +2 in the denominator guarantee that no probability is ever exactly 0 or 1 (if there is at least one example of each class, but even with zero examples, the probability would be 0.5).
Laplace smoothing is essential in practice, especially when m is large and some feature values may not appear in certain classes.
Making Predictions with Naive Bayes
For a new input x=(x1,x2,…,xm), compute:
Because the product of many probabilities can underflow numerically, we usually work with logarithms:
The argmax yields the predicted label (0 or 1). Note that the values are not probabilities because we omitted the normalization constant — but the comparison is valid.
Example with two features: Suppose x1 = likes Star Wars, x2 = likes Harry Potter, predict y = likes Lord of the Rings. A new user comes in with x1=1, x2=0. We compute:
For y=0: P(y=0)⋅P(x1=1∣y=0)⋅P(x2=0∣y=0)
For y=1: P(y=1)⋅P(x1=1∣y=1)⋅P(x2=0∣y=1)
Choose the y that gives the larger value (or larger log-sum).
Application: Spam Classification
One of the first major successes of Naive Bayes was spam email detection, pioneered by (among others) Meron Sahami at Google.
Features: presence or absence of each word in the English language (e.g., 100,000 words). xj=1 if the j-th word appears in the email, 0 otherwise.
Label: y=1 for spam, y=0 for ham (non-spam).
The number of features is huge, making the naive Bayes assumption essential.
Laplace smoothing is critical: many words never appear in spam training data, so MLE would give zero probability, killing predictions. Smoothing keeps probabilities finite.
Training: count, for spam emails, the fraction that contain each word; for ham emails, the same. Then for a new email, compute the argmax and classify.
Evaluation: hold out a test set of labeled emails. Common metrics:
Accuracy: fraction of correctly classified emails.
Precision: among emails predicted as spam, what fraction actually are spam? (How trustworthy is a "spam" prediction?)
Recall: among actual spam emails, what fraction were correctly identified? (How many spam messages are caught?)
High precision means few false positives (important emails misclassified as spam). High recall means few false negatives (spam slipping through). There is often a trade-off. With enough data, Naive Bayes achieved very high recall and precision in Gmail.
Evaluation of Classifiers
Held-Out Test Set
After training on a training set, we evaluate on a separate test set (or validation set) where the true labels are known but not used during training. The classifier predicts labels for test examples, and we compare against the truth.
Metrics
Accuracy: total test examplesnumber of correct predictions
Precision (for class 1, e.g., spam): true positives+false positivestrue positives
Recall (for class 1): true positives+false negativestrue positives
Where:
True positives: predicted 1 and actually 1
False positives: predicted 1 but actually 0
False negatives: predicted 0 but actually 1
In spam classification, precision matters because you don't want important emails in spam folder; recall matters because you want to catch as much spam as possible.
Summary
Naive Bayes is a classification algorithm based on Bayes' theorem with a conditional independence assumption among features given the label.
Training is simply counting: estimate P(y) and P(xj∣y) from data.
MLE uses raw counts; MAP with Laplace smoothing adds one imaginary count to avoid zero probabilities.
Prediction uses argmax of P(y)∏P(xj∣y), often computed in log space.
Despite its "naive" assumption, it works well for many problems, especially text classification (spam, sentiment) where the number of features is large.
Evaluate using held-out test set with metrics like accuracy, precision, and recall.

Ask anything
We've read this whole study set, so questions about it get the best answers.