∑GATE DA Atlas

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.

Section 68 practice questions

The Bayes classifier

Given features x\mathbf x, predict the class with the largest posterior probability (the MAP rule from Bayes' theorem):

y^=arg⁡max⁡yP(y∣x)=arg⁡max⁡yP(x∣y) P(y).\hat y = \arg\max_y P(y \mid \mathbf x) = \arg\max_y P(\mathbf x\mid y)\,P(y).

This Bayes-optimal rule minimises the error rate — if we knew the true distributions. The difficulty is estimating P(x∣y)P(\mathbf x\mid y): with dd binary features the joint distribution has 2d−12^d - 1 parameters per class.

The naive assumption

Assume the features are conditionally independent given the class:

P(x1,…,xd∣y)=∏j=1dP(xj∣y)⟹y^=arg⁡max⁡y P(y)∏j=1dP(xj∣y).P(x_1, \dots, x_d\mid y) = \prod_{j=1}^dP(x_j\mid y) \quad\Longrightarrow\quad \hat y = \arg\max_y\ P(y)\prod_{j=1}^dP(x_j\mid y).

Now each P(xj∣y)P(x_j \mid y) is a one-dimensional distribution estimated by simple counting. Parameters grow linearly in dd instead of exponentially.

Y X₁ X₂ X₃ X_d …
The class causes each feature; given the class, features carry no information about each other.

Training = counting

QuantityEstimate
Prior P(y=c)P(y = c)fraction of training examples in class cc
Categorical feature P(xj=v∣c)P(x_j = v\mid c)#(xj=v,y=c)#(y=c)\dfrac{\#(x_j = v, y = c)}{\#(y = c)}
Multinomial word probability P(w∣c)P(w\mid c)count of w in class ctotal words in class c\dfrac{\text{count of } w \text{ in class } c}{\text{total words in class } c}
Gaussian featureclass mean μjc\mu_{jc} and variance σjc2\sigma^2_{jc}

Training is a single pass over the data: O(nd)O(nd).

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-α\alpha) smoothing:

P(xj=v∣c)=#(xj=v,c)+α#(c)+α kj,P(x_j = v\mid c) = \frac{\#(x_j = v, c) + \alpha}{\#(c) + \alpha\,k_j},

where kjk_j is the number of possible values of xjx_j (vocabulary size ∣V∣\lvert V\rvert for words). α=1\alpha = 1 is add-one smoothing.

Variants

VariantFeaturesLikelihood
Bernoulli NBbinary (word present/absent)∏jpjcxj(1−pjc)1−xj\prod_j p_{jc}^{x_j}(1 - p_{jc})^{1 - x_j} — absent words count too
Multinomial NBcounts (word frequencies)∏wP(w∣c)nw\prod_w P(w\mid c)^{n_w}
Gaussian NBcontinuous∏j12πσjcexp⁡(−(xj−μjc)22σjc2)\prod_j\frac{1}{\sqrt{2\pi}\sigma_{jc}}\exp\left(-\frac{(x_j - \mu_{jc})^2}{2\sigma_{jc}^2}\right)

Log space: compute log⁡P(y)+∑jlog⁡P(xj∣y)\log P(y) + \sum_j\log P(x_j\mid y) to avoid underflow. Multinomial and Bernoulli NB are then linear classifiers in the features (log-probabilities act as weights).

Strengths and weaknesses

StrengthsWeaknesses
very fast to train and predict, scales to huge vocabulariesthe independence assumption is usually false
works with little data; few parametersprobability estimates are poorly calibrated (overconfident) when features are correlated
handles many features and missing values gracefullycorrelated 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.

Q1NAT2 marks

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).

Q2MCQ1 mark

The "naive" assumption in naive Bayes is that

Q3NAT2 marks

In a multinomial naive Bayes text classifier, a word never appears in the 100 word tokens of class cc, and the vocabulary has 50 words. With Laplace (add-one) smoothing, its estimated probability P(word∣c)P(\text{word}\mid c) is ______ (round to four decimals).

Q4MSQ2 marks

Which statements about naive Bayes are correct?

One or more options are correct.

Q5NAT2 marks

In Gaussian naive Bayes, a feature has class-conditional distribution N(0,1)N(0, 1) for class cc. The likelihood (density value) p(x=2∣c)p(x = 2 \mid c) is ______ (round to three decimals).

Q6NAT2 marks

A Bernoulli naive Bayes model has K=2K = 2 classes and d=10d = 10 binary features. The number of independent parameters it estimates is ______.

Q7MCQ1 mark

Naive Bayes implementations sum log-probabilities instead of multiplying probabilities in order to

Q8NAT2 marks

For an observation x\mathbf x, class A has prior 0.7 and likelihood P(x∣A)=0.2P(\mathbf x\mid A) = 0.2; class B has prior 0.3 and P(x∣B)=0.5P(\mathbf x\mid B) = 0.5. The posterior probability of class B is ______ (round to two decimals).