Naive Bayes (Classification)

Bayes plus a questionable independence assumption — and it works anyway.

What is Naive Bayes?

AnalogyDefinition

Imagine you're an experienced mail sorter. After years of work, you know: Letters with 'FREE' and 'WINNER' usually end up in the trash.

Naive Bayes works the same way: The algorithm learns which words are typical for spam and which are typical for regular emails. For each new email, it checks the words and calculates: How likely is this spam?

The 'Naive' part means: We pretend the words have nothing to do with each other. This isn't quite correct, but works surprisingly well!

This demo shows how a Naive Bayes spam filter analyzes emails word by word. Watch how the spam probability changes with each word.

Key Concepts
  • Prior (Initial Belief):The base probability that any random email is spam - before we read it.
  • Likelihood (Evidence Strength):How typical is each word for spam vs. legitimate emails?
  • Posterior (Final Result):The updated spam probability after we've analyzed the words.
The 'Naive' Assumption

We pretend all words are independent of each other. 'FREE' and 'WINNER' are considered separately, even though they often appear together. This simplification makes calculation easy and still works well in practice.

Why Is It Called 'Spam'?

The term comes from a 1970 sketch by British comedy group Monty Python. In a café, a group of Vikings repeatedly chant the word 'SPAM' (a canned meat brand) until it drowns out all other conversations. Just like in the sketch, spam emails flood our inboxes with unwanted, repetitive messages.

The probabilities above are not magic — they were learned from real emails. Try it yourself: sort the examples below into Spam or Ham. Your filter builds itself from word counts, and you can test it right after.

CONGRATULATIONS! You have won 500,000 dollars in the lottery! Click here right now to claim your prize — urgent, free today only!
My crypto robot earns 10,000 dollars a day — guaranteed! Click immediately for free access. Top secret, do not share!
Exclusive offer — today only! Earn millions with our secret system. Start free immediately and secure your money!
Urgent: click here for a free credit. Guaranteed without checks, instantly available. Limited offer!
Hi team, the meeting tomorrow at 10 a.m. is confirmed. Please prepare the report for the project. Best regards
Hello, a quick question about the project: can we schedule a meeting for tomorrow? Please let me know briefly. Thanks
Dear team, the report for the customer is in the attachment. Discussion about it on Wednesday in the office. Greetings from accounting.
Hi colleague, the boss is asking about the document. Can you prepare it by tomorrow? Thanks for your work!
Marked as ham: 0 Marked as spam: 0
You need at least 1 spam and 1 ham mail to activate the filter.

Spam Detector

What the probability journey shows

The spam meter is a horizontal scale: not spam on the left, spam on the right. A round marker travels along it while the filter reads the message word by word.

What you see
A horizontal bar, green on the left for harmless mail, red on the right for spam, with a scale from 0 to 100 percent in between. A round marker sits on it and shifts its colour from green through yellow to red, depending on where it stands.
What happens
The words of the message pass through the check one after another. Each word nudges the marker a little to the right if it looks suspicious, or to the left if it seems harmless. The marker jumps across the bar step by step until every word has had its turn.
What you can do
Pick a sample mail or type your own text, then start the journey with Start and halt it with Pause. Use Step to move word by word, and Reset to begin again. The base-rate slider shifts where the marker starts.
What to watch for
No single word decides on its own. The filter gathers many small clues and adds them up into one overall probability. Only where the marker comes to rest, left or right of the middle, is the verdict made.

Spam-O-Meter

HAM (Legitimate)SPAM
Ready for analysis
Ready for analysis

Controls

Prior: Base Spam Rate 50%
LowHigh
Change the prior to see how the base assumption affects classification.
On: you guess first, then Bayes calculates.
Keyboard: Space = Start/Pause, Right arrow = Step, R = Reset

Word Scanner

Known Words in Dictionary
🚨Spam Indicators
free sp4m win winner million guaranteed urgent click instant money now secret exclusive limited offer euro dollar credit earn claim immediately
Ham Indicators
meeting project colleague colleagues schedule appointment team report question thanks regards best tomorrow office work boss client clients document attachment

Naive Bayes Explained

TheoryPseudo-CodeStep by StepFlow Diagram

Bayes' Theorem

Naive Bayes is based on Bayes' Theorem by Thomas Bayes (1763): P(A|B) = P(B|A) × P(A) / P(B). It calculates the probability of a hypothesis (e.g., "Spam") given certain observations (e.g., words in an email).

The "naive" assumption: All features (words) are independent of each other. This isn't quite true - "free money" appears together more often than by chance. Yet the algorithm works surprisingly well!

Key Concepts

  • Prior P(Spam): Base probability of spam (e.g., 20% of all emails)
  • Likelihood P(Word|Spam): How likely does a word appear in spam?
  • Evidence P(Word): How often does the word appear overall?
  • Posterior P(Spam|Words): Final spam probability after analysis

Why 'Naive'?

The independence assumption simplifies calculation enormously: Instead of P(W₁,W₂,...|Spam), we simply calculate P(W₁|Spam) × P(W₂|Spam) × ... This enables fast classification even with many features.

Advantages

  • Fast: Training and prediction are very efficient
  • Low data requirements: Works well even with small datasets
  • Interpretable: You can see which words contribute to the decision
  • Robust: Insensitive to irrelevant features

Applications

Spam filters (Gmail, Outlook), sentiment analysis (positive/negative), document classification, medical diagnosis, recommendation systems, and speech recognition.

Try the demo! Enter an email and see step by step how the algorithm calculates the spam probability.

Test Your Knowledge

Question 1 / 4
Not completed

What does 'Naive' mean in 'Naive Bayes'?

Select one answer
Answer Key: 1) B · 2) C · 3) A · 4) D