Foundations to Frontier
Tests
D. Classical Machine Learning › D3 Probabilistic and Bayesian Classifiers

Gaussian Naive Bayes

Gaussian naive Bayes is the simplest generative classifier: it assumes that, within each class, every feature is an independent Gaussian. Training is a single pass that computes per-class means and variances; prediction adds up per-feature log-likelihoods. It needs very little data, handles many features and missing values gracefully, and is often a surprisingly accurate classifier. But its probabilities become overconfident when features are correlated, because shared evidence is counted repeatedly. This page covers the model, log-space computation, variance smoothing, its decision boundaries, when the independence assumption does and does not hurt, and how it relates to LDA and QDA. In the exercises you implement Gaussian naive Bayes with log-sum-exp; then you diagnose a thruster from redundant sensors and measure how its accuracy and calibration change as sensors are duplicated.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Score each clue separately, then multiply

To decide whether a thruster is degraded, a naive approach asks of each sensor separately: “how typical is this pressure for a degraded thruster? for a nominal one?” It then multiplies the answers together, as if the sensors told independent stories. That is naive Bayes. It is “naive” because sensors usually are not independent. Yet it is fast, needs little data, and often picks the right class. The catch is that when two sensors repeat the same clue, it counts that clue twice and becomes overconfident.

Naive Bayes: pretend the features are independent
true P(orange | x) = 0.702
naive Bayes P(orange | x) = 0.823
weights: true (0.78, 0.78) · naive (1.40, 1.40)
Solid: true class ellipses; dashed: what naive Bayes assumes. With ρ = 0 they coincide and naive Bayes is exact. As ρ grows, both features carry the same information, but naive Bayes adds their evidence as if it were independent: its probabilities become too extreme (compare at probe (0.6, 0.5)), even though along the diagonal its decision usually stays right.
Why engineers care

With dozens of telemetry channels and only a handful of labelled faults, there is far too little data to estimate correlations. Naive Bayes needs only a mean and a variance per channel per class, trains in one pass, and is easy to update on board. Knowing its overconfidence problem tells you when to trust its probabilities.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Naive Bayes assumes features are…
  2. 2. Gaussian naive Bayes estimates, per class and feature…
  3. 3. In the demo, with strongly correlated features, naive Bayes probabilities are…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Naive Bayes by hand

Training data with two features:

  • class 0: (1, 5), (2, 6), (3, 7);
  • class 1: (10, 1), (11, 2), (12, 3).

The class means are (2, 6) and (11, 2). Every within-class variance is 2/3, and the priors are ½ each.

Classify x = (5, 5). The per-feature log-likelihood terms −(x − μ)²/(2σ²) are:

Feature 1 Feature 2 Sum
Class 0 −(5 − 2)²/(4/3) = −6.75 −(5 − 6)²/(4/3) = −0.75 −7.5
Class 1 −(5 − 11)²/(4/3) = −27 −(5 − 2)²/(4/3) = −6.75 −33.75

The normalising terms are equal, so the log-odds are 26.25 in favour of class 0, and P(class 0 | x) ≈ 1 − 4·10⁻¹². Working in logs is essential: multiplying many small densities underflows to 0.

The steps in plain English:

  1. For each class, compute each feature’s mean and variance, and the class prior.
  2. Add a tiny “variance smoothing” so no variance is 0.
  3. For a new point, add up the log-density of each feature under each class, plus the log-prior.
  4. Turn the class scores into probabilities with log-sum-exp normalisation (a stable softmax).
  5. Predict the highest-scoring class. Treat the probabilities with caution if the features are correlated.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Within-class variance of 1, 2, 3 (dividing by n)?
  2. 2. Why compute in log space?
  3. 3. A missing feature can be handled by…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

The model

Assumption: p(x | y = k) = ∏ⱼ N(xⱼ; μₖⱼ, σ²ₖⱼ). This is a Gaussian with a diagonal, class-specific covariance.

Classifier: ŷ = argmaxₖ [log πₖ + Σⱼ log N(xⱼ; μₖⱼ, σ²ₖⱼ)].

Boundaries.

  • With class-specific variances, the boundaries are quadratic: Gaussian naive Bayes is QDA with diagonal covariances.
  • With variances shared across classes (σ²ₖⱼ = σ²ⱼ), the boundaries are linear and the model is “diagonal LDA”.

Parameters: 2Kp + (K − 1), linear in p. QDA needs O(Kp²). That is why naive Bayes works with many features and few examples.

Practicalities:

  • var_smoothing adds ε·max variance to all variances (scikit-learn’s default ε = 10⁻⁹), preventing divide-by-zero for constant features.
  • Missing values: skip the missing feature’s term.
  • Online updates: keep running means and variances per class (Welford’s algorithm); partial_fit does this.
from sklearn.naive_bayes import GaussianNB
nb = GaussianNB(var_smoothing=1e-9).fit(X, y)
nb.predict_proba(X_new); nb.theta_, nb.var_           # per-class means and variances
nb.partial_fit(X_batch, y_batch, classes=[0, 1])      # streaming updates

Use it when there are many features and few labelled examples, a fast baseline is needed, or the features are close to independent given the class. Do not trust its probabilities with redundant or strongly correlated features; calibrate them (D0.10) or use LDA or logistic regression.

🐍 Exercise 1: Gaussian naive Bayes
Implement Gaussian naive Bayes with variance smoothing, joint log-likelihoods, log-sum-exp posteriors and predictions; check the hand example, a 50/50 midpoint, numerical stability far from the data, and that with truly independent Gaussian features it matches the Bayes classifier.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Gaussian naive Bayes is QDA with…
  2. 2. Number of parameters grows with p…
  3. 3. var_smoothing prevents…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Why “naive” works, and when it does not

Classification versus probability (Domingos and Pazzani, 1997). For a correct decision, naive Bayes only needs the ranking of class scores to be right, not the probabilities themselves. Dependencies often distort both classes’ scores in similar ways, so the argmax survives even when the probabilities are badly wrong. Naive Bayes can therefore be optimal for 0–1 loss well outside the region where its independence assumption holds.

Double counting. If a feature is copied r times, naive Bayes multiplies its log-likelihood ratio by r. A log-odds of 0.69 (2:1) becomes 0.69r: with r = 4, that is 16:1. Exercise 2 shows exactly this: accuracy stays flat while log loss and ECE explode as copies are added.

Generative versus discriminative. Ng and Jordan (2002) showed that naive Bayes approaches its (possibly worse) asymptotic error with only O(log p) examples, while logistic regression needs O(p). Naive Bayes often wins with small samples, and logistic regression wins with large ones.

Fixes:

  • calibrate the scores (isotonic or Platt);
  • decorrelate features first (PCA or whitening);
  • select features to remove redundancy;
  • relax the independence assumption: TAN and semi-naive Bayes (D3.35) add a few dependencies;
  • weight features (weighted naive Bayes).

Interview questions.

  • Why can naive Bayes be accurate but badly calibrated? The argmax is robust to multiplicative distortions that affect both classes alike, but the posterior magnitudes are not.
  • How do you handle a continuous feature that is clearly non-Gaussian? Transform it (log, Box–Cox), discretise it (then use multinomial or categorical NB), or use a kernel density per class.
📝 Quick check (Level 3)
0/3 answered
  1. 1. A 2:1 likelihood ratio counted 4 times gives odds of…
  2. 2. Domingos & Pazzani showed naive Bayes can be optimal for 0–1 loss…
  3. 3. With small samples, naive Bayes often beats logistic regression because it…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Diagnosing a thruster from redundant sensors

History.

  • Bayes’ rule (Bayes, 1763; Laplace, 1774) underlies the method.
  • “Idiot’s Bayes” classifiers appeared in 1960s pattern recognition and medical diagnosis (Warner et al., 1961).
  • Duda and Hart (1973) formalised them.
  • Domingos and Pazzani (1997) explained naive Bayes’ surprising accuracy.
  • Ng and Jordan (2002) analysed generative versus discriminative learning curves.
  • Zadrozny and Elkan (2001) calibrated naive Bayes scores.

Exercise 1. The hand example’s posteriors sum to 1, the midpoint (6.5, 4) gets exactly 50/50, and log-sum-exp keeps the posteriors finite for x = (10⁶, 10⁶). With truly independent Gaussian features (n = 3000, 3 classes), naive Bayes reaches accuracy 0.824 against 0.821 for the true Bayes classifier.

Exercise 2: redundant sensors. Setup: 3 physical signals, each read by r near-identical sensors; 300 training runs; 30 repetitions. Each cell shows accuracy / log loss / ECE.

Copies r Gaussian NB LDA Logistic regression
1 0.724 / 0.545 / 0.032 0.725 / 0.543 / 0.032 0.725 / 0.543 / 0.033
2 0.723 / 0.617 / 0.103 0.719 / 0.550 / 0.037 0.721 / 0.548 / 0.036
4 0.722 / 0.990 / 0.189 0.713 / 0.560 / 0.043 0.718 / 0.551 / 0.038
8 0.724 / 1.752 / 0.230 0.700 / 0.588 / 0.062 0.714 / 0.560 / 0.040
  • Naive Bayes’ accuracy does not move (0.722–0.724), and with 8 copies it is the most accurate of the three. Its ranking of runs stays correct.
  • Its probabilities become wildly overconfident. Log loss rises from 0.545 to 1.75, worse than guessing 50/50 (0.693), and ECE rises from 0.03 to 0.23. Each copy re-counts the same evidence.
  • LDA and logistic regression model the correlation and stay calibrated, but lose a little accuracy as the number of parameters grows (24 nearly collinear features from 300 runs).
  • For fault alarms, naive Bayes’ decisions are fine. For fault probabilities that feed risk calculations, calibrate them or remove redundancy first.
🐍 Exercise 2 (research): redundant sensors and overconfidence
Simulate thruster runs where three physical signals are each read by 1, 2, 4 or 8 near-identical sensors; compare Gaussian naive Bayes, LDA and logistic regression by test accuracy, log loss and expected calibration error.

Naive Bayes in AI, robotics and aerospace

  • Fault detection with few examples: per-channel Gaussian class models of telemetry.
  • On-board learning: one-pass, constant-memory updates of class means and variances.
  • Sensor fusion baselines: “independent evidence” fusion is naive Bayes; its overconfidence with correlated sensors is a known hazard.
  • Text and log classification: multinomial and Bernoulli variants (D3.32–33).

References

  1. P. Domingos, M. Pazzani (1997). “On the optimality of the simple Bayesian classifier under zero-one loss.” Machine Learning 29(2–3).
  2. A. Y. Ng, M. I. Jordan (2002). “On discriminative vs. generative classifiers: a comparison of logistic regression and naive Bayes.” NeurIPS.
  3. B. Zadrozny, C. Elkan (2001). “Obtaining calibrated probability estimates from decision trees and naive Bayesian classifiers.” ICML.
  4. D. J. Hand, K. Yu (2001). “Idiot’s Bayes — not so stupid after all?” International Statistical Review 69(3).
  5. R. O. Duda, P. E. Hart, D. G. Stork (2001). Pattern Classification, 2nd ed. Wiley.