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

<div data-sim="specx" data-spec="{&#34;head&#34;:&#34;Gaussian naive Bayes on Fisher&#39;s iris flowers&#34;,&#34;sub&#34;:&#34;Forty iris flowers, versicolor (0) or virginica (1), by petal length and width. Fit one bell curve per class and feature, then move the new flower and the prior and watch the posterior.&#34;,&#34;params&#34;:[{&#34;id&#34;:&#34;prior&#34;,&#34;label&#34;:&#34;Prior P(virginica)&#34;,&#34;min&#34;:0.05,&#34;max&#34;:0.95,&#34;step&#34;:0.05,&#34;value&#34;:0.5,&#34;note&#34;:&#34;0.5 = the share in the data (20 of 40)&#34;},{&#34;id&#34;:&#34;q_len&#34;,&#34;label&#34;:&#34;New flower: petal length&#34;,&#34;min&#34;:3,&#34;max&#34;:7,&#34;step&#34;:0.1,&#34;value&#34;:5,&#34;unit&#34;:&#34;cm&#34;},{&#34;id&#34;:&#34;q_wid&#34;,&#34;label&#34;:&#34;New flower: petal width&#34;,&#34;min&#34;:0.8,&#34;max&#34;:2.6,&#34;step&#34;:0.1,&#34;value&#34;:1.7,&#34;unit&#34;:&#34;cm&#34;},{&#34;id&#34;:&#34;thr&#34;,&#34;label&#34;:&#34;Decision threshold&#34;,&#34;min&#34;:0.1,&#34;max&#34;:0.9,&#34;step&#34;:0.05,&#34;value&#34;:0.5,&#34;note&#34;:&#34;Call virginica when the posterior reaches this&#34;}],&#34;data&#34;:{&#34;columns&#34;:[&#34;petal_len&#34;,&#34;petal_wid&#34;,&#34;virginica&#34;],&#34;rows&#34;:[[4.7,1.4,0],[4.5,1.5,0],[4.9,1.5,0],[4,1.3,0],[4.6,1.5,0],[4.5,1.3,0],[4.7,1.6,0],[3.3,1,0],[4.6,1.3,0],[3.9,1.4,0],[3.5,1,0],[4.2,1.5,0],[4,1,0],[4.7,1.4,0],[3.6,1.3,0],[4.4,1.4,0],[4.5,1.5,0],[4.1,1,0],[4.5,1.5,0],[3.9,1.1,0],[6,2.5,1],[5.1,1.9,1],[5.9,2.1,1],[5.6,1.8,1],[5.8,2.2,1],[6.6,2.1,1],[4.5,1.7,1],[6.3,1.8,1],[5.8,1.8,1],[6.1,2.5,1],[5.1,2,1],[5.3,1.9,1],[5.5,2.1,1],[5,2,1],[5.1,2.4,1],[5.3,2.3,1],[5.5,1.8,1],[6.7,2.2,1],[6.9,2.3,1],[5,1.5,1]],&#34;note&#34;:&#34;Fisher&#39;s iris data (UCI, as shipped with scikit-learn load_iris): the first 20 versicolor and first 20 virginica flowers, petal length and width in cm. Paste any two numeric features and a 0/1 label.&#34;},&#34;row&#34;:[{&#34;id&#34;:&#34;a1&#34;,&#34;label&#34;:&#34;len × y&#34;,&#34;expr&#34;:&#34;petal_len*virginica&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;a2&#34;,&#34;label&#34;:&#34;len² × y&#34;,&#34;expr&#34;:&#34;petal_len^2*virginica&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;b1&#34;,&#34;label&#34;:&#34;len × (1−y)&#34;,&#34;expr&#34;:&#34;petal_len*(1 - virginica)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;b2&#34;,&#34;label&#34;:&#34;len² × (1−y)&#34;,&#34;expr&#34;:&#34;petal_len^2*(1 - virginica)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;c1&#34;,&#34;label&#34;:&#34;wid × y&#34;,&#34;expr&#34;:&#34;petal_wid*virginica&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;c2&#34;,&#34;label&#34;:&#34;wid² × y&#34;,&#34;expr&#34;:&#34;petal_wid^2*virginica&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;e1&#34;,&#34;label&#34;:&#34;wid × (1−y)&#34;,&#34;expr&#34;:&#34;petal_wid*(1 - virginica)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;e2&#34;,&#34;label&#34;:&#34;wid² × (1−y)&#34;,&#34;expr&#34;:&#34;petal_wid^2*(1 - virginica)&#34;,&#34;hide&#34;:true}],&#34;derived&#34;:[{&#34;id&#34;:&#34;n1&#34;,&#34;label&#34;:&#34;Virginica flowers&#34;,&#34;expr&#34;:&#34;sum(virginica)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;n0&#34;,&#34;label&#34;:&#34;Versicolor flowers&#34;,&#34;expr&#34;:&#34;n - n1&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;mu1_l&#34;,&#34;label&#34;:&#34;Mean petal length, virginica&#34;,&#34;expr&#34;:&#34;sum(a1)/n1&#34;,&#34;tex&#34;:&#34;\\mu_{c,j} = \\frac{1}{N_c}\\sum_{i:\\,y_i=c} x_{ij}&#34;},{&#34;id&#34;:&#34;mu0_l&#34;,&#34;label&#34;:&#34;Mean petal length, versicolor&#34;,&#34;expr&#34;:&#34;sum(b1)/n0&#34;},{&#34;id&#34;:&#34;v1_l&#34;,&#34;label&#34;:&#34;Variance of length, virginica&#34;,&#34;expr&#34;:&#34;sum(a2)/n1 - mu1_l^2&#34;,&#34;tex&#34;:&#34;\\sigma^2_{c,j} = \\frac{1}{N_c}\\sum_{i:\\,y_i=c} x_{ij}^2 - \\mu_{c,j}^2&#34;},{&#34;id&#34;:&#34;v0_l&#34;,&#34;label&#34;:&#34;Variance of length, versicolor&#34;,&#34;expr&#34;:&#34;sum(b2)/n0 - mu0_l^2&#34;},{&#34;id&#34;:&#34;mu1_w&#34;,&#34;label&#34;:&#34;Mean petal width, virginica&#34;,&#34;expr&#34;:&#34;sum(c1)/n1&#34;},{&#34;id&#34;:&#34;mu0_w&#34;,&#34;label&#34;:&#34;Mean petal width, versicolor&#34;,&#34;expr&#34;:&#34;sum(e1)/n0&#34;},{&#34;id&#34;:&#34;v1_w&#34;,&#34;label&#34;:&#34;Variance of width, virginica&#34;,&#34;expr&#34;:&#34;sum(c2)/n1 - mu1_w^2&#34;},{&#34;id&#34;:&#34;v0_w&#34;,&#34;label&#34;:&#34;Variance of width, versicolor&#34;,&#34;expr&#34;:&#34;sum(e2)/n0 - mu0_w^2&#34;},{&#34;id&#34;:&#34;q_llr&#34;,&#34;label&#34;:&#34;New flower: log-odds of virginica&#34;,&#34;expr&#34;:&#34;(-0.5*ln(v1_l) - (q_len - mu1_l)^2/(2*v1_l) - 0.5*ln(v1_w) - (q_wid - mu1_w)^2/(2*v1_w)) - (-0.5*ln(v0_l) - (q_len - mu0_l)^2/(2*v0_l) - 0.5*ln(v0_w) - (q_wid - mu0_w)^2/(2*v0_w)) + ln(prior/(1 - prior))&#34;,&#34;tex&#34;:&#34;\\ell = \\ln\\frac{\\pi}{1-\\pi} + \\sum_j \\Big[\\ln \\mathcal N(x_j;\\mu_{1,j},\\sigma^2_{1,j}) - \\ln \\mathcal N(x_j;\\mu_{0,j},\\sigma^2_{0,j})\\Big]&#34;},{&#34;id&#34;:&#34;q_post&#34;,&#34;label&#34;:&#34;New flower: P(virginica)&#34;,&#34;expr&#34;:&#34;sigmoid(q_llr)&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;P(y=1\\mid x) = \\sigma(\\ell) = \\frac{1}{1+e^{-\\ell}}&#34;}],&#34;row2&#34;:[{&#34;id&#34;:&#34;llr&#34;,&#34;label&#34;:&#34;log-odds&#34;,&#34;expr&#34;:&#34;(-0.5*ln(v1_l) - (petal_len - mu1_l)^2/(2*v1_l) - 0.5*ln(v1_w) - (petal_wid - mu1_w)^2/(2*v1_w)) - (-0.5*ln(v0_l) - (petal_len - mu0_l)^2/(2*v0_l) - 0.5*ln(v0_w) - (petal_wid - mu0_w)^2/(2*v0_w)) + ln(prior/(1 - prior))&#34;,&#34;tex&#34;:&#34;\\ln \\mathcal N(x;\\mu,\\sigma^2) = -\\tfrac12\\ln(2\\pi\\sigma^2) - \\frac{(x-\\mu)^2}{2\\sigma^2}&#34;},{&#34;id&#34;:&#34;post&#34;,&#34;label&#34;:&#34;P(virginica)&#34;,&#34;expr&#34;:&#34;sigmoid(llr)&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;hit&#34;,&#34;label&#34;:&#34;correct&#34;,&#34;expr&#34;:&#34;ifelse(ge(post, thr), virginica, 1 - virginica)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;ll&#34;,&#34;label&#34;:&#34;log loss&#34;,&#34;expr&#34;:&#34;-(virginica*ln(post + 1e-12) + (1 - virginica)*ln(1 - post + 1e-12))&#34;}],&#34;post&#34;:[{&#34;id&#34;:&#34;acc&#34;,&#34;label&#34;:&#34;Training accuracy&#34;,&#34;expr&#34;:&#34;mean(hit)&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;errors&#34;,&#34;label&#34;:&#34;Flowers misclassified&#34;,&#34;expr&#34;:&#34;n - sum(hit)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;loss&#34;,&#34;label&#34;:&#34;Mean log loss&#34;,&#34;expr&#34;:&#34;mean(ll)&#34;,&#34;tex&#34;:&#34;L = -\\frac1n\\sum_i \\big[y_i\\ln p_i + (1-y_i)\\ln(1-p_i)\\big]&#34;}],&#34;chart&#34;:{&#34;title&#34;:&#34;Class bell curves for petal length, and the posterior&#34;,&#34;x&#34;:{&#34;var&#34;:&#34;t&#34;,&#34;label&#34;:&#34;Petal length (cm)&#34;,&#34;from&#34;:3,&#34;to&#34;:7.5},&#34;y&#34;:{&#34;from&#34;:0,&#34;to&#34;:1.05},&#34;series&#34;:[{&#34;label&#34;:&#34;Versicolor density p(length | 0)&#34;,&#34;expr&#34;:&#34;phi((t - mu0_l)/sqrt(v0_l))/sqrt(v0_l)&#34;},{&#34;label&#34;:&#34;Virginica density p(length | 1)&#34;,&#34;expr&#34;:&#34;phi((t - mu1_l)/sqrt(v1_l))/sqrt(v1_l)&#34;},{&#34;label&#34;:&#34;P(virginica) from length alone&#34;,&#34;expr&#34;:&#34;sigmoid(ln(prior/(1 - prior)) - 0.5*ln(v1_l) - (t - mu1_l)^2/(2*v1_l) + 0.5*ln(v0_l) + (t - mu0_l)^2/(2*v0_l))&#34;,&#34;dash&#34;:true}],&#34;points&#34;:{&#34;x&#34;:&#34;petal_len&#34;,&#34;y&#34;:&#34;virginica&#34;,&#34;label&#34;:&#34;Flowers (0 = versicolor, 1 = virginica)&#34;},&#34;fit&#34;:{&#34;x&#34;:&#34;petal_len&#34;,&#34;y&#34;:&#34;post&#34;,&#34;label&#34;:&#34;Posterior from length and width&#34;},&#34;mark&#34;:&#34;q_len&#34;},&#34;level&#34;:&#34;beginner&#34;,&#34;bars&#34;:{&#34;title&#34;:&#34;P(virginica) for each training flower&#34;,&#34;y&#34;:&#34;post&#34;},&#34;steps&#34;:[{&#34;title&#34;:&#34;Count the classes&#34;,&#34;text&#34;:&#34;{n1} virginica and {n0} versicolor flowers. The prior P(virginica) is set to {prior}.&#34;,&#34;show&#34;:[&#34;n1&#34;,&#34;n0&#34;]},{&#34;title&#34;:&#34;Fit one bell curve per class and feature&#34;,&#34;text&#34;:&#34;Petal length: versicolor mean {mu0_l} cm, variance {v0_l}; virginica mean {mu1_l} cm, variance {v1_l}. Petal width: {mu0_w} and {mu1_w} cm.&#34;,&#34;show&#34;:[&#34;mu0_l&#34;,&#34;v0_l&#34;,&#34;mu1_l&#34;,&#34;v1_l&#34;,&#34;mu0_w&#34;,&#34;mu1_w&#34;]},{&#34;title&#34;:&#34;Score the new flower under each class&#34;,&#34;text&#34;:&#34;For petal length {q_len} cm and width {q_wid} cm, evaluate each class&#39;s two normal densities. Naive Bayes treats length and width as independent given the class, so the densities multiply (their logs add).&#34;},{&#34;title&#34;:&#34;Combine with the prior&#34;,&#34;text&#34;:&#34;The log-odds is ln(prior odds) plus the sum of log-density differences: {q_llr}.&#34;,&#34;show&#34;:[&#34;q_llr&#34;]},{&#34;title&#34;:&#34;Turn log-odds into a probability&#34;,&#34;text&#34;:&#34;The sigmoid gives P(virginica) = {q_post:p}; at threshold {thr} the flower is called virginica when this reaches the threshold.&#34;,&#34;show&#34;:[&#34;q_post&#34;]},{&#34;title&#34;:&#34;Check it on the training flowers&#34;,&#34;text&#34;:&#34;Training accuracy is {acc:p} ({errors} misclassified), with mean log loss {loss}.&#34;,&#34;show&#34;:[&#34;acc&#34;,&#34;errors&#34;,&#34;loss&#34;]}]}"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## 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

1. **Count the classes.** N₀ versicolor and N₁ virginica flowers. The prior π = P(virginica) is usually the class share, here 20 / 40 = 0.5.
2. **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 μ².
3. **Score a new flower.** Evaluate ln N(x; μ, σ²) = −½ ln(2πσ²) − (x − μ)² / (2σ²) for each feature under each class.
4. **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)].
5. **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](/ai-simulations/tfm-calibration). [Logistic regression](/ai-simulations/logistic-regression-gradient-descent) 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](/ai-simulations/precision-recall-threshold). See also the [naive Bayes tool page](/tools/naive-bayes).

## Sources

- [scikit-learn: Naive Bayes (Gaussian naive Bayes)](https://scikit-learn.org/stable/modules/naive_bayes.html)
- [UCI Machine Learning Repository: Iris](https://archive.ics.uci.edu/dataset/53/iris)
- [scikit-learn GaussianNB API (var_smoothing)](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html)
- [Naive Bayes classifier (Wikipedia)](https://en.wikipedia.org/wiki/Naive_Bayes_classifier)
