How does Naive Bayes work?
- Authors
- Name
- Amit Shekhar
- Published on
Naive Bayes is a method that sorts things into groups using Bayes' theorem, a rule about chance, while naively assuming each clue is independent of the others.
In this blog, we will learn about how Naive Bayes works. We will also see Bayes' theorem behind it, why it is called "naive", how it is trained and how it makes a prediction using a spam filter example, how Laplace smoothing fixes the zero problem, its three types, and where it works well and where it fails.
We will cover the following:
- What is Naive Bayes?
- Bayes' theorem in plain words
- Why it is called "Naive"
- A real example: the spam filter
- Step by step: training the model
- Step by step: making a prediction
- The problem of the zero, and Laplace smoothing
- The three types of Naive Bayes
- Why Naive Bayes is so popular
- Limitations of Naive Bayes
- Where Naive Bayes is used today
I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
I teach AI and Machine Learning at Outcome School.
Let's get started.
What is Naive Bayes?
Naive Bayes is a method that a computer uses to sort things into groups. Putting things into groups is called classification.
Naive Bayes looks at something, weighs the clues, and guesses which group it most likely belongs to.
Suppose we give it an email. It can guess whether the email is "spam" or "not spam". Suppose we give it a message. It can guess whether the message is "happy" or "angry".
The "Bayes" part of the name comes from Bayes' theorem, a rule about chance that we will learn soon. The "Naive" part comes from one simple assumption it makes, which we will also learn.
Bayes' theorem in plain words
Bayes' theorem is a rule that helps us update our guess when we see new evidence.
It tells us this: start with what we already believe, then adjust that belief based on the new clue we just saw.
Let's say we hear someone coughing. Before hearing the cough, we believe most people are healthy. After hearing the cough, we slightly raise our belief that the person may have a cold. We did not become certain. We just updated our guess using the new clue.
That is exactly what Bayes' theorem does, but with numbers instead of feelings.
The rule can be written like below:
P(spam | clue) = (P(clue | spam) x P(spam)) / P(clue)
Here, we can see the symbol P(spam | clue). We read it as "the probability of spam, given the clue". The straight bar | means "given that we already saw". So the whole left side asks: now that we have seen this clue, how likely is spam?
On the right side, P(clue | spam) asks how often this clue shows up inside spam. P(spam) asks how common spam is in general. We multiply them together. The bottom part, P(clue), is how likely the clue is overall, and it just scales the answer so it stays a proper probability.
For sorting into groups, the bottom part is the same for every group, so we mainly care which group gets the bigger top value. The biggest one wins.
Why it is called "Naive"
A real email has many clues, which means many words. Words can be linked. For example, the words "free" and "money" often appear together in spam.
Tracking how every word affects every other word would be very hard and very slow.
So, Naive Bayes makes one simplifying assumption. It pretends that every clue is independent, which means each clue acts on its own and does not depend on the others.
This is the naive part. In real life, words are not fully independent. The method knows this is not perfectly true, but it pretends anyway.
Let's see the idea like below:
REALITY (hard) NAIVE BAYES (easy)
words are linked each word stands alone
+-------------------------+ +-------------------------+
| "free" <---> "money" | | "free" (alone) |
| ^ ^ | | "money" (alone) |
| | | | | "now" (alone) |
| "now" <---------+ | +-------------------------+
+-------------------------+ |
| ↓
↓ multiply each word's
too many links to track chance together
Here, we can see the shortcut. On the left, the real world links words together, which is hard to track. On the right, Naive Bayes cuts every link and looks at each word alone, so it can multiply the chances one by one.
Why pretend? Because this shortcut makes the math very simple and very fast. We can just look at each word on its own and multiply the results together.
Even though the assumption is not fully correct, Naive Bayes still works well in practice for many tasks.
Planning your move into AI?
No matter which tech domain you work in today, with your effort, you can transition to these roles:
- AI Engineer
- Forward Deployed Engineer
- AI Agent Engineer
- Harness Engineer
- Voice AI Engineer
- AI Architect
- LLM Engineer
- LLM Inference Engineer
- Gen AI Engineer
- MLOps Engineer
- ML Engineer
- Deep Learning Engineer
Our AI and Machine Learning Program at Outcome School takes you there.
A real example: the spam filter
The best way to learn this is by taking an example. Let's build a tiny spam filter in our head.
A spam filter is a tool that reads an email and decides if it is spam, which means unwanted junk mail, or not spam, which means a normal, wanted email.
Naive Bayes treats this as a simple question for each email: which is bigger, the score for "spam" or the score for "not spam"? The bigger score wins, and the email goes into that group.
To answer this, the method needs two things. First, it needs to learn from old emails. This is called training. Second, it needs to score a new email. This is called prediction. Let's look at each.
Step by step: training the model
Training means showing the method many old examples so it can learn the patterns.
We give it a pile of old emails that humans have already labeled as "spam" or "not spam". The method then counts.
It counts how many emails are spam, and how many are not. This gives the prior, which is our starting belief before reading any words.
Then, for each word, it counts how often that word appears in spam emails, and how often it appears in normal emails.
Let's see the flow like below:
Old labeled emails
+-------------------------------------+
| "win free money now" -> SPAM |
| "team lunch at noon" -> NOT SPAM |
| "free prize click" -> SPAM |
| "project meeting" -> NOT SPAM |
+-------------------------------------+
|
↓
Count everything
|
↓
+-------------------------------------+
| Prior: spam = 2/4, not spam = 2/4 |
| Word "free" in spam -> 2 times |
| Word "free" in not spam -> 0 times |
| Word "meeting" in spam -> 0 times |
| ... and so on for every word ... |
+-------------------------------------+
Here, we can see that training is just careful counting. We count how common each group is, and we count how often each word shows up inside each group. The method stores all these counts. That stored table of counts is the trained model.
To learn Supervised and Unsupervised Learning, Model Development and Training, and the rest of the Machine Learning fundamentals from the ground up, check out our AI and Machine Learning Program at Outcome School.
Step by step: making a prediction
Now a new email arrives, and we must guess its group.
Let's say the new email says "free money".
The method now does this for each group, "spam" and "not spam".
Step 1: Start with the prior, our starting belief for that group.
Step 2: For each word in the email, look up how likely that word is inside this group.
Step 3: Multiply all of these together. Because of the naive assumption, we can multiply each word's chance, one after another.
Step 4: Do this for both groups, and compare the two scores.
Let's see it as below:
New email: "free money"
SPAM score = P(spam) x P("free"|spam) x P("money"|spam)
NOT SPAM score = P(not spam) x P("free"|not spam) x P("money"|not spam)
|
↓
Compare the two scores
|
↓
Bigger score wins -> the email's group
Here, we can see the full prediction. We build one score per group by multiplying the starting belief with each word's chance. Then we pick the group with the bigger score. If the spam score is bigger, the email is marked as spam.
This is how Naive Bayes turns a pile of words into a single decision.
Once the filter starts making these decisions, we also need a way to check how often it is right. We have a detailed blog on how a confusion matrix works that explains this step by step.
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
The problem of the zero, and Laplace smoothing
What if a word in the new email never appeared in the spam training emails? Then its count is 0, so its probability is 0.
But we are multiplying all the word chances together. And anything multiplied by 0 becomes 0. So one single unseen word would crush the whole score down to zero, even if every other word clearly pointed to spam.
One missing word should not cancel all the other clues.
So, here comes Laplace smoothing to the rescue.
Laplace smoothing adds a small number, usually 1, to every word count. So a count of 0 becomes 1, a count of 5 becomes 6, and so on. The total count is also adjusted, so that the probabilities still add up to 1.
Let's see the fix like below:
WITHOUT smoothing WITH smoothing (add 1)
+---------------------------+ +-----------------------------+
| "free" count = 2 | | "free" count = 2 + 1 = 3 |
| "money" count = 1 | | "money" count = 1 + 1 = 2 |
| "lunch" count = 0 -> 0 | | "lunch" count = 0 + 1 = 1 |
+---------------------------+ +-----------------------------+
| |
↓ ↓
one zero makes the whole every word keeps a small,
score 0 non-zero say in the score
Here, we can see the difference smoothing makes. On the left, the unseen word "lunch" has a count of 0, and that single zero drags the whole multiplied score down to 0. On the right, we add 1 to every count, so even "lunch" now has a tiny value, and the score stays alive.
This trick makes sure no probability is ever exactly zero. Now an unseen word makes the score small, but not dead. The other clues still get their say.
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
The three types of Naive Bayes
Naive Bayes comes in three common types. Each one fits a different kind of clue. The scikit-learn library provides all three.
Multinomial Naive Bayes: This type cares about counts, like how many times each word appears. It is the most common choice for text, such as spam filters and news sorting.
Bernoulli Naive Bayes: This type cares only about presence, which means whether a word appears at all, yes or no. It does not care how many times. This works well for short texts.
Gaussian Naive Bayes: This type is for clues that are smooth numbers, like height, weight, or temperature, instead of word counts. It assumes these numbers follow a bell-shaped pattern.
Let's see how to pick one like below:
What kind of clue do we have?
|
+-------------------+-------------------+
| | |
↓ ↓ ↓
word counts yes / no presence measured numbers
(how many times) (does it appear?) (height, weight)
| | |
↓ ↓ ↓
Multinomial Bernoulli Gaussian
Naive Bayes Naive Bayes Naive Bayes
Here, we can see that the choice comes down to one question: what kind of clue do we have? Word counts point us to Multinomial, simple yes or no clues point us to Bernoulli, and smooth measured numbers point us to Gaussian.
Why Naive Bayes is so popular
Reason 1: It is very fast. Training is just counting, and prediction is just a few multiplications. It can train and predict very quickly.
Reason 2: It needs little data. It can learn useful patterns even from a small number of examples.
Reason 3: It is simple to build and easy to understand. There are very few moving parts, so it is a great first method to learn.
Reason 4: It handles many clues well. Even with thousands of words as clues, it stays fast, because it treats each clue on its own.
Limitations of Naive Bayes
Naive Bayes is useful, but it is not the right tool for every job.
Its biggest weakness is the naive assumption itself. Because it treats each word on its own, it cannot learn that "free money" together is more likely spam than "free" and "money" seen apart. It misses the relationships between clues.
Also, its final probability numbers are often not very precise. The ranking is usually good, which means it picks the right group, but the exact confidence value may be off.
For tasks that need a deep understanding of meaning and word order, modern Transformer models, such as BERT, do much better.
Here, we can see the simple trade-off. Naive Bayes gives up deep understanding and, in return, gives us speed, simplicity, and low cost.
Where Naive Bayes is used today
Let's see where Naive Bayes is used.
- Spam filtering in email, which was its most famous early use.
- Sentiment analysis, which means deciding if a review or message is positive or negative.
- News and document sorting, which means putting articles into topics like sports, business, or health.
- Quick first-pass filters inside bigger systems, where speed matters and a fast, cheap guess is enough.
Even though heavier models now win on the hardest language tasks, Naive Bayes remains a low-cost option. It is fast, it is light, and it often serves as the baseline that newer methods are measured against.
Frequently Asked Questions
Why does Naive Bayes still work if its assumption is wrong?
Naive Bayes only needs to pick the right group, not give exact probabilities. Its ranking is usually good, so it picks the right group, even though the exact confidence value may be off. Words are not fully independent in real life, but the shortcut still works well in practice for many tasks, such as spam filtering.
What is the prior in Naive Bayes?
The prior is our starting belief about each group before reading any words. During training, Naive Bayes gets it by counting how many of the labeled examples are in each group. For example, if 2 out of 4 old emails are spam, the prior for spam is 2/4.
Which Naive Bayes type should we use for text?
Multinomial Naive Bayes is the most common choice for text, such as spam filters and news sorting, because it cares about how many times each word appears. Bernoulli Naive Bayes only checks whether a word appears at all, and works well for short texts. Gaussian Naive Bayes is for smooth numbers, not word counts.
Is Naive Bayes better than Transformer models like BERT?
No. For tasks that need a deep understanding of meaning and word order, Transformer models such as BERT do much better. Naive Bayes trades that deep understanding for speed, simplicity, and low cost, and it often serves as the baseline that newer methods are measured against.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
