← All AI/ML articles

Naive Bayes, visualised.

The classifier behind early spam filters. It starts from a prior belief, lets every clue (every word) nudge that belief up or down, and treats each clue as independent, which is naive, but surprisingly effective.

1. Build an email2. Watch clues add up3. Break the naive assumption
Build an email
Tap the words that appear in the email. Words you leave untapped count as absent, and absence is evidence too.
Spam share = the prior: how common spam is before reading anything. α = Laplace smoothing (explained below).
not spam50%spam
What each word contributes (present and absent)
Behind the scenes · 1

Bayes’ rule, with a naive twist

We want P(spam | words). Bayes’ rule flips it into things we can count: P(spam) × P(words | spam). Computing P(words | spam) for a whole combination of words is impossible, so Naive Bayes assumes each word is independent given the class and simply multiplies single-word probabilities. This page uses Bernoulli Naive Bayes: every word in the vocabulary is a yes/no feature. A word that is in the email contributes P(word | class). A word that is not in the email contributes 1 − P(word | class). Do this for both classes and compare.


Only the ratio matters, so the two scores are normalised to sum to 100%. Multiplying many small numbers underflows, so real software adds logarithms instead. That is exactly what the coloured bars show: each word, present or absent, adds or subtracts log-odds. Leaving absent words out would give a different (and wrong) answer. For example, a completely empty email is not simply “the prior”: not seeing any spammy words is itself evidence for normal mail.

Behind the scenes · 2

Where the numbers come from: counting

“Training” is just counting, in 40 spam and 60 normal emails, how many contain each word. Words in your email are highlighted. Notice the zeros: “winner” never appeared in a normal email and “project” never in spam. With α = 0 a zero probability multiplies everything to zero and one unseen word vetoes the whole decision.


Cyan-outlined cells are the ones your email actually uses: P(word | class) for words that are in it, 1 − P(word | class) for words that are not.
Laplace smoothing, precisely

For each class, a word is either present or absent in an email: two outcomes. Laplace smoothing adds α imaginary emails to each outcome. So if a word appears in c of N emails in a class, it is treated as appearing in c + α and missing from (N − c) + α emails, out of N + 2α in total:


P(word | class) = (c + α) ÷ (N + 2α) P(no word | class) = (N − c + α) ÷ (N + 2α) = 1 − P(word | class)

The 2α in the denominator is not arbitrary: it is what makes the two probabilities add up to exactly 1. This is the formula for Bernoulli Naive Bayes. Multinomial Naive Bayes (which counts how many times each word occurs) uses a different one: (word count + α) ÷ (total words in class + α·V), where V is the vocabulary size. Here is the calculation for the first word you selected:


Behind the scenes · 3

Numbers instead of words: Gaussian Naive Bayes

For measurements like height or income, Naive Bayes describes each class with a bell curve per feature (a mean and a spread). Each class becomes an axis-aligned ellipse. Drag the white star to see the maths for any point, or click empty space to add points.


class Aclass BDashed ellipses = 1σ and 2σ of each class · white dots = decision boundary
Accuracy0
Mean confidence0
Over-confidence0
Points0
The maths at the white star

What the model learned (just means and spreads)

Behind the scenes · 4

When “naive” hurts

Raise Feature link above so x₁ and x₂ move together inside each class, with the classes separated along that same direction. The model still multiplies the two likelihoods as if they were separate evidence, so it counts the same clue twice. Accuracy barely changes, but its confidence soars past its real accuracy. Naive Bayes is often a good classifier but a poor source of trustworthy probabilities.

Practical

When is Naive Bayes a good idea?


Very fast

Training is counting and prediction is a few multiplications. It scales to huge data.

Little data is fine

It needs few examples per class because it estimates simple one-feature statistics.

Great for text

Spam filtering, topic tagging and sentiment started here and still make strong baselines.

Probabilities are rough

The independence shortcut makes the probabilities overconfident. Trust the ranking more than the number.

Experiment

Try it yourself


The zero-count veto

A friendly email with one never-seen word, and no smoothing. One word overrules everything.

Spam is rare

Same spammy words, but spam is only 5% of mail. The prior fights back.

Count the clue twice

Make the two features move together and watch confidence outrun accuracy.

Check yourself

Three quick questions


Key takeaways

  • Naive Bayes picks the class with the highest prior × product of feature likelihoods.
  • “Naive” means it assumes features are independent given the class. That is rarely true, yet it often works.
  • Training is counting (or measuring a mean and spread per class), so it is extremely fast.
  • Bernoulli Naive Bayes scores every word in the vocabulary: present words use P(word | class), absent words use 1 − P(word | class).
  • Laplace smoothing adds α to both “present” and “absent” counts, so a single unseen word cannot force a probability of zero.
  • Correlated features get counted twice, so the probabilities become overconfident.
← Back to all articles