🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #140

Gaussian Naive Bayes

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

\[ P(x_i \mid y) = \frac{1}{\sqrt{2\pi\sigma_y^2}}\ e^{-\frac{(x_i-\mu_y)^2}{2\sigma_y^2}} \]

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).

\[ P(5.5\mid \text{pass}) = \frac{1}{\sqrt{2\pi(1.25)}}e^{-\frac{(5.5-6.5)^2}{2(1.25)}} \approx 0.357 \times e^{-0.4} \approx 0.239 \] \[ P(5.5\mid \text{fail}) = \frac{1}{\sqrt{2\pi(1.25)}}e^{-\frac{(5.5-2.5)^2}{2(1.25)}} \approx 0.357 \times e^{-3.6} \approx 0.0097 \]
\[ P(\text{pass}\mid 5.5) \propto 0.5(0.239) = 0.1196, \quad P(\text{fail}\mid 5.5) \propto 0.5(0.0097) = 0.0049 \] \[ \text{Normalized: } \frac{0.1196}{0.1196+0.0049} \approx \mathbf{0.961} \]

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.

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Gaussian Naive Bayes – FAQs

Quick answers about learning Gaussian Naive Bayes in Machine Learning.

This free note from CodingNow 2.0 explains Gaussian Naive Bayes in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Gaussian Naive Bayes, is 100% free with no signup required.
With focused practice, most students grasp Gaussian Naive Bayes in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now