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 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; 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 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
- 1Estimate priors .
- 2Gaussian: per class and feature, mean and variance (plus a small ε). Multinomial: smoothed word frequencies per class.
- 3For a new : score each class by .
- 4Predict the highest score; turn scores into probabilities with a softmax (log-sum-exp).
The maths
Decision rule
Gaussian likelihood
Multinomial with Laplace smoothing
Posterior from log scores
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…
Check your understanding
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.