Skip to content
Lambdian

Naive Bayes

Bayes' rule with a bold simplification: features independent given the class. Fast, tiny and great for text.

Animated explanation

The idea

Naive Bayes classifies by asking: which class makes this data most probable, given how common each class is? That is Bayes' rule. The difficulty is estimating P(x∣c)P(\mathbf{x}\mid c) for a whole feature vector. The "naive" trick assumes the features are independent once you know the class, so the joint probability is just a product of per-feature probabilities, each easy to estimate by counting.

The assumption is almost always false (words in an email are correlated), yet the ranking of classes is often right, which is all a classifier needs. Naive Bayes trains in one pass over the data, handles thousands of features, and is still a strong baseline for text.

Three flavours

  • Gaussian NB: continuous features; each class gets a mean and variance per feature.
  • Multinomial NB: word counts; P(w∣c)P(w\mid c) is the class's word frequency (with smoothing).
  • Bernoulli NB: word presence/absence; also penalises words that are missing.

All three are generative: they model how each class produces data, then invert with Bayes' rule.

Smoothing and log space

A word never seen in spam during training would get P(w∣spam)=0P(w\mid\text{spam}) = 0 and veto spam forever. Laplace (add-α) smoothing adds a pseudo-count α to every word. And since multiplying hundreds of probabilities underflows to 0, always sum log probabilities instead.

Calibration

Because correlated features are counted as independent evidence, naive Bayes is typically overconfident: its probabilities crowd near 0 and 1. The predicted class is often fine; if you need trustworthy probabilities, calibrate them (Platt scaling or isotonic regression) on validation data.

The algorithm, step by step

  1. 1
    Estimate priors P(c)=nc/nP(c) = n_c / n.
  2. 2
    Gaussian: per class and feature, mean μcj\mu_{cj} and variance σcj2\sigma^2_{cj} (plus a small ε). Multinomial: smoothed word frequencies per class.
  3. 3
    For a new x\mathbf{x}: score each class by log⁡P(c)+∑jlog⁡P(xj∣c)\log P(c) + \sum_j \log P(x_j\mid c).
  4. 4
    Predict the highest score; turn scores into probabilities with a softmax (log-sum-exp).

The maths

Decision rule

c^=arg⁡max⁡c log⁡P(c)+∑jlog⁡P(xj∣c)\hat c = \arg\max_c\ \log P(c) + \sum_{j}\log P(x_j\mid c)

Gaussian likelihood

log⁡P(xj∣c)=−12log⁡(2πσcj2)−(xj−μcj)22σcj2\log P(x_j\mid c) = -\tfrac12\log(2\pi\sigma_{cj}^2) - \frac{(x_j - \mu_{cj})^2}{2\sigma_{cj}^2}

Multinomial with Laplace smoothing

θ^cw=Ncw+αNc+α∣V∣\hat\theta_{cw} = \frac{N_{cw} + \alpha}{N_c + \alpha|V|}

Posterior from log scores

P(c∣x)=esc∑c′esc′P(c\mid\mathbf{x}) = \frac{e^{s_c}}{\sum_{c'} e^{s_{c'}}}

Strengths and weaknesses

Strengths

  • Trains in a single pass; tiny model
  • Excellent for text and very high-dimensional sparse data
  • Works with small training sets
  • Naturally multi-class

Weaknesses

  • Independence assumption makes probabilities overconfident
  • Cannot learn feature interactions
  • Gaussian NB is poor for non-Gaussian features

Reach for it when…

Text classification (spam, sentiment, topics), quick baselines, and when training data is small or arrives as a stream.

Check your understanding

Question 1

Q1What exactly is 'naive' about naive Bayes?

Question 2

Q2A test email contains a word never seen in training spam. Without smoothing…

Question 3

Q3Why sum log-probabilities instead of multiplying probabilities?

Lambdian Lab

Gaussian and multinomial naive Bayes

Implement both models in log space.

  • GaussianNB: fit(X, y) stores classes_, log priors, means and variances (add var_smoothing = 1e-9 × the largest feature variance, as scikit-learn does); predict_log_proba(X) returns normalised log posteriors; predict(X).
  • MultinomialNB(alpha=1.0): same interface for count data, with Laplace smoothing.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.