Gaussian Naive Bayes handles continuous numeric features by assuming each feature follows a normal distribution within each class — using the mean and standard deviation of that class's training values to compute how likely a new value is.
Formula
This is exactly the normal distribution formula, with \(\mu_y\) and \(\sigma_y^2\) being the mean and variance of feature \(x_i\) computed only from the training examples belonging to class \(y\) — a separate Gaussian is fit per class.
Worked Example
Predicting pass/fail from hours studied. Pass class: \([5,6,7,8]\), mean \(\mu=6.5\), variance \(\sigma^2=1.25\). Fail class: \([1,2,3,4]\), mean \(\mu=2.5\), variance \(\sigma^2=1.25\) (same spread, by construction, in this example).
New student studied 5.5 hours. Priors: \(P(\text{pass})=P(\text{fail})=0.5\) (4 examples each).
5.5 hours is much closer to the "pass" class mean (6.5) than the "fail" class mean (2.5), so the model predicts pass with about 96.1% confidence — matching intuition.
import numpy as np
def gaussian_likelihood(x, mean, var):
coeff = 1 / np.sqrt(2 * np.pi * var)
exponent = np.exp(-((x - mean) ** 2) / (2 * var))
return coeff * exponent
p_pass = 0.5 * gaussian_likelihood(5.5, 6.5, 1.25)
p_fail = 0.5 * gaussian_likelihood(5.5, 2.5, 1.25)
print(p_pass / (p_pass + p_fail)) # 0.961
# scikit-learn
from sklearn.naive_bayes import GaussianNB
X_train = np.array([[5],[6],[7],[8],[1],[2],[3],[4]])
y_train = np.array([1,1,1,1,0,0,0,0])
model = GaussianNB()
model.fit(X_train, y_train)
print(model.predict_proba([[5.5]])) # matches the hand calculation closely
Why the Normality Assumption Usually Isn't a Dealbreaker
Real features are rarely perfectly normal — but Gaussian NB tends to be fairly robust to moderate departures from normality, since (as with the independence assumption) it mostly needs to rank classes correctly, not model the true distribution exactly. Strongly skewed features can still hurt performance meaningfully, though — log-transforming a skewed feature before applying Gaussian NB is a common, effective fix.
Practical Use Cases
- Classification problems with continuous numeric features that are roughly bell-shaped per class
- Fast baseline classification on numeric/tabular data
Common Mistakes
- Applying Gaussian NB to strongly skewed features without any transformation, when the normality assumption is badly violated.
- Applying Gaussian NB to count or binary data — Multinomial or Bernoulli Naive Bayes are the correct variants for those feature types.
Interview Relevance
Q: "Why does Gaussian Naive Bayes fit a separate mean and variance per class, rather than one overall?" Because the model needs to compare how likely an observed feature value is under each class specifically — a value close to class A's mean but far from class B's mean is exactly the signal that should point the prediction toward class A, which requires class-conditional statistics, not a single global one.
Practice Question
Using the pass/fail example, compute \(P(x\mid\text{pass})\) and \(P(x\mid\text{fail})\) for a new student who studied exactly 3.5 hours, and determine the predicted class.