Lesson 06
Naive Bayes
Nine beginner steps on one spam email: prior, likelihood, a posterior of 0.83, three Naive Bayes types, and Laplace smoothing.
What You Will Learn
Nine steps. One email the whole way. Click a step to jump.
- 1. What is Naive Bayes?
- 2. Types of Naive Bayes
- 3. Where is it used?
- 4. The variants, not four different algorithms
- 5. What problems does Naive Bayes face?
- 6. How those problems are handled
- 7. Key Takeaways
- 8. Mind Map
- 9. Quiz
1. What is Naive Bayes?
An email arrives: "Congratulations. You won a free phone." A person does not read an equation. They notice free and won and think "this is probably spam."
Naive Bayes is that habit, written as numbers. It is a supervised classifier. You show it old emails already labelled spam or not spam. For a new email it scores each label and picks the larger score.
The score is a probability from Bayes' rule. Four words are enough.
| Name | In this email | Our number |
|---|---|---|
| Prior | How common spam was before you read this email | P(spam) = 0.40 |
| Likelihood | How often spam emails contain the word free | P(free given spam) = 0.75 |
| Evidence | How often any email contains free | P(free) = 0.36 |
| Posterior | Chance this email is spam after you saw free | P(spam given free) = 0.83 |
The rule, in words:
posterior = likelihood × prior / evidence
P(spam given free) = P(free given spam) × P(spam) / P(free)
Fill the evidence before you divide. Not-spam is the rest of the old mail: 1 − 0.40 = 0.60. Among not-spam, only 0.10 contain free.
P(free) = 0.75 × 0.40 + 0.10 × 0.60 = 0.30 + 0.06 = 0.36
Now the posterior:
0.75 × 0.40 = 0.30
0.30 / 0.36 = 0.83
So if the only clue is the word free, about 83 emails in 100 like this are spam. Not-spam gets the rest: 1 − 0.83 = 0.17. The picture at the top is that one division.
It is called naive because it treats each word as separate news. Free, winner, and prize often travel together. The method multiplies their chances anyway, as if they did not. That shortcut is usually wrong in real sentences, and it is still useful because the multiplication stays small and fast.
2. Types of Naive Bayes
Same rule. The likelihood changes with the kind of column you have.
| Variant | The columns look like | Spam story |
|---|---|---|
| Gaussian | Numbers that can fall anywhere on a scale | Hours since the email was sent, or the length in words. Each class is summarised by an average and a spread. |
| Multinomial | Counts | The word free appears 3 times, offer appears 1 time. This is the usual text model. |
| Bernoulli | Yes or no | The email contains free, or it does not. Same for offer and win. |
A fourth name, Complement Naive Bayes, is still this family. It helps when one class, such as spam, is much rarer than the other. It is not a new kind of learning.
3. Where is it used?
| Job | The classes |
|---|---|
| Spam | Spam or not spam. This lesson. |
| Sentiment | Positive or negative review |
| News | Sport, politics, technology |
| Documents | Which folder a file belongs in |
| Language | English, Hindi, Telugu, from the letters and words |
| Symptoms | Which recorded category fits, when each symptom is present or absent |
The method likes many columns and a clear label. It is weak when you need a careful model of how the columns depend on each other.
4. The variants, not four different algorithms
People say "Gaussian Naive Bayes" and "Multinomial Naive Bayes" as if they were strangers. They are one classifier with a different way to write the likelihood.
| You have | Use |
|---|---|
| Marks, hours, temperature | Gaussian |
| Word counts in emails | Multinomial |
| A checklist of words, each 0 or 1 | Bernoulli |
| Text, and one class is rare | Complement, still Naive Bayes |
Support vector machines draw a street between classes. Naive Bayes does not draw a street. It asks which class makes this email more probable.
5. What problems does Naive Bayes face?
Stay with spam.
A zero count. The training spam never used the word lottery. Then P(lottery given spam) is written as 0. A new email says lottery. The whole spam score is a product, and a product that includes 0 is 0. Spam can never win, no matter what the other words say.
Words that travel together. Free, prize, and winner are one sales pitch, not three independent facts. Multiplying three big spam chances counts the same pitch three times. The spam score gets over-confident.
A shape that was assumed. Gaussian Naive Bayes treats each number as a bell curve inside each class. Email length is often a bell. A column that is mostly zeros, with a few huge values, is not. The likelihood is then the wrong shape, and the posterior inherits that mistake.
6. How those problems are handled
Zero count: add one. This is Laplace smoothing. It is not the weight fine from regularization. Nothing about m or α on a line. You only stop a count from being zero.
Suppose the spam bag has 10 word tokens, and the vocabulary is 4 words: free, winner, offer, lottery. Lottery was never seen, so its count is 0. Add 1 to the count, and add 1 for every vocabulary word to the bag size.
P(lottery given spam) = (0 + 1) / (10 + 4) = 1 / 14
About 0.07, not 0. The product can still prefer spam or not spam. An unseen word is only a small chance, not a veto.
Words that travel together. Smoothing does not fix this. You can drop a repeated word, or keep the method and remember the score is rough. If the dependence is the whole point of the job, use a model that is allowed to learn it.
Wrong shape. Do not force Gaussian onto word counts. Use multinomial or Bernoulli for the email. If no variant matches the columns, leave Naive Bayes.
7. Key Takeaways
- Naive Bayes picks a class by which posterior is larger.
- Posterior = likelihood × prior / evidence.
- On this email, free and a spam prior of 0.40 give 0.83.
- Naive means the words are multiplied as if they were separate.
- Gaussian, multinomial, and Bernoulli are the same idea on different columns.
- A zero count kills the product. Laplace adds 1 so the chance is small, not zero.
- That add-one step is not L1 or L2 regularization.
8. Mind Map
Cover a corner and say it back. If the 0.83 still comes from 0.30 divided by 0.36, the first step is yours.
9. Quiz
The questions use this email. Prior 0.40, free-given-spam 0.75, evidence 0.36, posterior 0.83. They also ask which variant fits word counts, and what Laplace is for.
Check your understanding
Naive Bayes Quiz
Naive Bayes is mainly used for which task?
1 / 7