Naive Bayes Classifier
The Bayes-optimal (MAP) classifier, the naive conditional-independence assumption and why it makes learning tractable, parameter estimation by counting, worked spam example, zero counts and Laplace smoothing, Bernoulli, multinomial and Gaussian variants, log-space computation, and strengths and weaknesses.
The Bayes classifier
Given features , predict the class with the largest posterior probability (the MAP rule from Bayes' theorem):
This Bayes-optimal rule minimises the error rate — if we knew the true distributions. The difficulty is estimating : with binary features the joint distribution has parameters per class.
The naive assumption
Assume the features are conditionally independent given the class:
Now each is a one-dimensional distribution estimated by simple counting. Parameters grow linearly in instead of exponentially.
Training = counting
| Quantity | Estimate |
|---|---|
| Prior | fraction of training examples in class |
| Categorical feature | |
| Multinomial word probability | |
| Gaussian feature | class mean and variance |
Training is a single pass over the data: .
Zero counts and Laplace smoothing
If a feature value never occurs with a class in training, its estimate is 0 and wipes out the entire product. Laplace (add-) smoothing:
where is the number of possible values of (vocabulary size for words). is add-one smoothing.
Variants
| Variant | Features | Likelihood |
|---|---|---|
| Bernoulli NB | binary (word present/absent) | — absent words count too |
| Multinomial NB | counts (word frequencies) | |
| Gaussian NB | continuous |
Log space: compute to avoid underflow. Multinomial and Bernoulli NB are then linear classifiers in the features (log-probabilities act as weights).
Strengths and weaknesses
| Strengths | Weaknesses |
|---|---|
| very fast to train and predict, scales to huge vocabularies | the independence assumption is usually false |
| works with little data; few parameters | probability estimates are poorly calibrated (overconfident) when features are correlated |
| handles many features and missing values gracefully | correlated features are effectively double-counted |
| strong baseline for text (spam, sentiment, topic) | outperformed by discriminative models with enough data |
Naive Bayes often classifies well even when its probabilities are off, because only the ranking of the classes matters for the decision.
Quick revision
Practice questions
GATE-style questions: 2 one-mark and 6 two-mark. Wrong MCQs cost ⅓ of their marks in the real exam; MSQ and NAT have no negative marking. Attempt each before opening the solution.
A spam filter is trained on 10 e-mails: 4 spam and 6 ham. The word "free" appears in 3 of the spam and 1 of the ham e-mails; the word "meeting" appears in 1 spam and 4 ham e-mails. Using naive Bayes on the presence of these two words (no smoothing), the posterior probability that an e-mail containing both words is spam is ______ (round to two decimals).
The "naive" assumption in naive Bayes is that
In a multinomial naive Bayes text classifier, a word never appears in the 100 word tokens of class , and the vocabulary has 50 words. With Laplace (add-one) smoothing, its estimated probability is ______ (round to four decimals).
Which statements about naive Bayes are correct?
One or more options are correct.
In Gaussian naive Bayes, a feature has class-conditional distribution for class . The likelihood (density value) is ______ (round to three decimals).
A Bernoulli naive Bayes model has classes and binary features. The number of independent parameters it estimates is ______.
Naive Bayes implementations sum log-probabilities instead of multiplying probabilities in order to
For an observation , class A has prior 0.7 and likelihood ; class B has prior 0.3 and . The posterior probability of class B is ______ (round to two decimals).