Classical ML

Naive Bayes

The ultra-fast probabilistic classifier that assumes feature independence and handles text baselines effortlessly.

🟢 beginner4 min readsupervisedbayesian
Naive Bayes applies Bayes' Theorem to classification under the 'naive' assumption that all features x_i are conditionally independent given class label y. This simplifies class posterior calculation to P(y | x) ∝ P(y) ∏ P(x_i | y). Variants include Gaussian Naive Bayes (continuous features), Multinomial Naive Bayes (word counts in text), and Bernoulli Naive Bayes (binary features). Laplace smoothing (additive α-smoothing) prevents zero-frequency zero probability traps.

The Naive Bayes Derivation

By Bayes' Theorem:

P(y∣x1,…,xd)=P(y)P(x1,…,xd∣y)P(x1,…,xd)P(y \mid x_1, \dots, x_d) = \frac{P(y) P(x_1, \dots, x_d \mid y)}{P(x_1, \dots, x_d)}

Under the Conditional Independence Assumption:

P(x1,…,xd∣y)=∏i=1dP(xi∣y)P(x_1, \dots, x_d \mid y) = \prod_{i=1}^d P(x_i \mid y)

Dropping denominator P(X)P(X) (constant for all classes yy):

y^=arg⁡max⁡yP(y)∏i=1dP(xi∣y)\hat{y} = \arg\max_y P(y) \prod_{i=1}^d P(x_i \mid y)

In log-space (to avoid floating-point underflow):

y^=arg⁡max⁡y[ln⁡P(y)+∑i=1dln⁡P(xi∣y)]\hat{y} = \arg\max_y \left[ \ln P(y) + \sum_{i=1}^d \ln P(x_i \mid y) \right]

Three Core Naive Bayes Variants

  1. Gaussian Naive Bayes: Continuous features xi∼N(μy,i,σy,i2)x_i \sim \mathcal{N}(\mu_{y,i}, \sigma_{y,i}^2). Uses Gaussian likelihood.
  2. Multinomial Naive Bayes: Text classification with word frequency counts xi∈{0,1,2,… }x_i \in \{0, 1, 2, \dots\}.
  3. Bernoulli Naive Bayes: Binary features xi∈{0,1}x_i \in \{0, 1\} (explicitly penalizes absence of expected words).

Laplace (Additive) Smoothing

Unsmoothed feature likelihood:

P(xi∣y)=Ny,iNyP(x_i \mid y) = \frac{N_{y, i}}{N_y}

If word ii never appeared in training class yy, Ny,i=0  ⟹  P(xi∣y)=0N_{y,i} = 0 \implies P(x_i \mid y) = 0.

Laplace Additive Smoothing (α=1\alpha = 1):

P(xi∣y)=Ny,i+αNy+α∣V∣P(x_i \mid y) = \frac{N_{y, i} + \alpha}{N_y + \alpha |V|}

Where ∣V∣|V| is the total vocabulary size.

Say this out loud

"Naive Bayes classifies samples by applying Bayes' Theorem under the conditional independence assumption P(x_1..x_d|y) = ∏ P(x_i|y). This turns high-dimensional joint estimation into fast 1D probability multiplications. We use Multinomial Naive Bayes for word counts and apply Laplace smoothing α = 1 to prevent unobserved words from zeroing out class probabilities."

Follow-ups to expect

Check yourself

Question 1 of 3

Why does a single unobserved word in a test document cause standard unsmoothed Naive Bayes to output zero probability for that entire class?

More in Classical ML

See all →
Bias–Variance Tradeoff4 minOverfitting vs Underfitting3 minLinear Regression4 min