Gaussian naive Bayes
Quick answer
Gaussian naive Bayes models continuous features using a normal density for each feature within each class. It combines those densities and a class prior to score a new observation. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Compare two log densities
from math import log,pix=2print([round(-.5*(log(2*pi)+(x-m)**2),6) for m in [1,5]])Expected output:
[-1.418939, -5.418939]With equal priors and variances, the higher log density favors mean 1.
How it works
Bayes rule compares class scores P(c) times P(x|c). Naive Bayes approximates the joint likelihood by a product of feature likelihoods conditional on the class. This is conditional independence, not independence across the entire dataset. Gaussian NB uses a separate mean and variance per class and feature. For value x, mean mu and positive variance v, the log density is -0.5*[ln(2piv)+(x-mu)^2/v]. Add these terms to log prior; the class with highest log joint score wins. A density may exceed one and is not itself a probability of an exact continuous value. Class frequency is a common prior estimate. For class means 1 and 5 with equal variances and priors, a query at 2 favors the first. Variances estimated by maximum likelihood divide by n rather than n-1. Zero variance makes the formula undefined; scikit-learn GaussianNB adds a variance stabilization term based on var_smoothing times the largest global training feature variance. This does not rescue a completely constant dataset with zero global variance; inspect degeneracy, remove unsuitable features or choose a better model. Correlated or duplicated features can count evidence repeatedly and produce overconfident estimates. Check predictions and calibration separately; Gaussian-shaped marginals alone do not establish conditional independence.
Fit a continuous classifier
from sklearn.naive_bayes import GaussianNBm=GaussianNB().fit([[0],[2],[4],[6]],[0,0,1,1])print(m.theta_.tolist())print(m.predict([[1],[5]]).tolist())Expected output:
[[1.0], [5.0]][0, 1]Training estimates class-specific centers; the two queries lie near different centers.
Common mistakes
Do not use the sample variance denominator n-1 when reproducing this maximum-likelihood estimate. Never evaluate log(0) or divide by zero variance. Do not claim calibrated probabilities simply because scores normalize.
Practice and next steps
Compute class means and maximum-likelihood variances for [0,2] and [4,6]. Explain what duplicated features do to the independence assumption. Complete the knowledge check and coding exercise. Next: Multinomial and Bernoulli naive Bayes.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Gaussian naive Bayes — knowledge check
Apply gaussian naive bayes to new examples.
What independence is assumed?
Which variance denominator is used for Gaussian maximum likelihood?
What is needed to evaluate a Gaussian density?
What does a duplicated correlated feature risk?
What is a class prior?