Gaussian naive Bayes on iris flowers: bell curves per class, priors and the posterior
Gaussian naive Bayes learns nothing but a mean and a variance per class and feature, yet it separates two hard-to-tell iris species with 95% accuracy. Move a new flower, change the prior and the threshold, and follow every number from bell curves to the final probability.
Interactive simulation. It needs JavaScript; the text below explains what it shows.
What you are looking at
The data are 40 flowers from Fisher's iris dataset: the first 20 Iris versicolor (label 0) and the first 20 Iris virginica (label 1), measured by petal length and petal width in centimetres. These two species overlap, which makes them the interesting pair; the third species, setosa, is easy to separate from both.
The classifier fits one normal (bell-shaped) curve per class and per feature: four curves in all, each just a mean and a variance. To classify a flower it asks how typical its petal length and width are under each species, multiplies those densities with the prior, and compares.
The sliders set the prior probability of virginica, the petal length and width of a new flower, and the decision threshold. The chart shows the two bell curves for petal length, the data points at 0 and 1, and a dashed curve: the posterior you would get from petal length alone. The fitted dots show the posterior that uses both features, and the vertical line marks the new flower's petal length. The bars show P(virginica) for every training flower.
An everyday picture: guessing whether a tall stranger plays basketball. You start from how common basketball players are (the prior), then ask how usual that height is among players and among everyone else (the likelihoods). Naive Bayes does this one feature at a time and multiplies the answers.
The algorithm
- Count the classes. N₀ versicolor and N₁ virginica flowers. The prior π = P(virginica) is usually the class share, here 20 / 40 = 0.5.
- Fit the bell curves. For each class c and feature j, the mean μ and the variance σ² are the maximum-likelihood estimates: the class average of x and the class average of x² minus μ².
- Score a new flower. Evaluate ln N(x; μ, σ²) = −½ ln(2πσ²) − (x − μ)² / (2σ²) for each feature under each class.
- Assume independence and add logs. Naive Bayes treats features as independent given the class, so the densities multiply, and their logs add. The log-odds is ℓ = ln(π / (1 − π)) + Σⱼ [ln N(xⱼ | virginica) − ln N(xⱼ | versicolor)].
- Convert to a probability. P(virginica | x) = σ(ℓ) = 1 / (1 + e^(−ℓ)), and the flower is called virginica when this reaches the threshold.
Worked example (default settings). The fitted means are 4.255 cm (versicolor) and 5.655 cm (virginica) for petal length, with variances 0.191 and 0.389, and 1.325 cm and 2.045 cm for petal width, with variances 0.038 and 0.071. The new flower has petal length 5.0 cm and width 1.7 cm. For length, the log-density is −0.998 under virginica and −1.542 under versicolor, a difference of +0.544. For width it is −0.432 against −1.139, a difference of +0.706. With a prior of 0.5 the prior term is 0, so ℓ = 0.544 + 0.706 = 1.250 and P(virginica) = 1 / (1 + e^(−1.250)) = 77.7%. On the 40 training flowers the model is right 95% of the time: it misses rows 27 (4.5 cm by 1.7 cm, posterior 23.1%) and 40 (5.0 cm by 1.5 cm, posterior 19.0%), two virginica flowers with versicolor-sized petals. The mean log loss is 0.089. These numbers match scikit-learn's GaussianNB with var_smoothing set to 0.
Things to try
- Lower the prior to 0.2. The same new flower drops to 46.6% and is now called versicolor: the evidence was not strong enough to beat a sceptical prior.
- Raise the prior to 0.8. The new flower rises to 93.3%, row 27 is now classified correctly, training errors fall to 1 and the log loss falls to 0.071.
- Keep the prior at 0.5 and lower the threshold to 0.2. Row 27 (23.1%) is now caught, and accuracy rises to 97.5%.
- Move the new flower's width from 1.7 to 1.5 cm. The posterior falls to 19.0%: the two features add their evidence, so width alone can swing the decision.
- Compare the dashed length-only curve with the fitted dots. With length alone the 50% point sits at 4.90 cm; adding width moves many flowers much closer to 0 or 1.
How it works in real systems
Naive Bayes is fast to train (one pass to compute means and variances), needs little data and handles many features, which is why it is still a common baseline for text classification and spam filtering, there usually in its multinomial or Bernoulli form. scikit-learn's GaussianNB adds a tiny variance floor, var_smoothing times the largest feature variance, so that a feature that never varies inside a class does not cause a division by zero.
The independence assumption is rarely true: petal length and width are strongly correlated, and naive Bayes counts that shared evidence twice. The ranking of classes is often still right, but the probabilities tend to be overconfident, so calibrate them before using them as risks; see the calibration simulation. Logistic regression fits the same log-odds form directly instead of building it from per-class densities, and choosing the cut-off is covered in precision and recall against the threshold. See also the naive Bayes tool page.