Search Knowledge

© 2026 LIBREUNI PROJECT

Machine Learning / Supervised Learning

Naive Bayes

Naive Bayes Classifier

Naive Bayes is a probabilistic classifier based on Bayes’ Theorem. It assumes conditional independence between features given the class.

Probabilistic Foundation: Bayes’ Theorem

We predict the class probability P(Ckx)P(C_k | \mathbf{x}) for an input feature vector x=[x1,x2,,xd]T\mathbf{x} = [x_1, x_2, \dots, x_d]^T using:

P(Ckx)=P(xCk)P(Ck)P(x)P(C_k | \mathbf{x}) = \frac{P(\mathbf{x} | C_k) P(C_k)}{P(\mathbf{x})}

The Conditional Independence Assumption

Estimating P(xCk)P(\mathbf{x} | C_k) directly requires massive datasets. The Naive Bayes classifier assumes that features are conditionally independent given the class:

P(xCk)=j=1dP(xjCk)P(\mathbf{x} | C_k) = \prod_{j=1}^{d} P(x_j | C_k)

Thus, the classification rule predicts the class that maximizes the numerator:

y^=argmaxkP(Ck)j=1dP(xjCk)\hat{y} = \arg\max_{k} P(C_k) \prod_{j=1}^{d} P(x_j | C_k)

The “Naive” Limitation

Features are rarely independent in practice, making the model “naive” (e.g., “machine” and “learning” are highly correlated), but it performs remarkably well on tasks like spam filtering.

Generative vs. Discriminative

Naive Bayes is a generative model because it models the joint distribution P(x,Ck)=P(xCk)P(Ck)P(\mathbf{x}, C_k) = P(\mathbf{x} | C_k) P(C_k), unlike discriminative models (e.g., Logistic Regression) which model P(Ckx)P(C_k | \mathbf{x}) directly.

Laplace Smoothing

If a feature value xjx_j never occurs with class CkC_k in training, the probability P(xjCk)P(x_j | C_k) is 00. Since we multiply feature probabilities, this zeroes out the entire class probability. We prevent this using Laplace Smoothing:

P(xj=vCk)=count(xj=v,Ck)+αcount(Ck)+αNjP(x_j = v | C_k) = \frac{count(x_j = v, C_k) + \alpha}{count(C_k) + \alpha \cdot N_j}

where NjN_j is the number of possible values for xjx_j, and α\alpha is the smoothing parameter.

Example: Classification with Gaussian Naive Bayes

The following example demonstrates training a Gaussian Naive Bayes model on continuous features:

python

Interactive Lab

Fit a Gaussian Naive Bayes classifier on continuous physical features and predict class probability distributions.

Step 1
Inspect the idea
Step 2
Edit the program
Step 3
Run and compare

Exercise

Test your understanding of the independence assumption:

What is the main drawback of the Naive Bayes conditional independence assumption?

Prior Probabilities

Priors P(Ck)P(C_k) reflect baseline class rates, acting as regularizers that bias predictions toward dominant classes.

References & Further Reading

Previous Module Regularization Theory