Naive Bayes
Naive Bayes makes a smart guess using probability. It looks at clues in an example and calculates which group is more likely.
Build a tiny email spam detector and watch its scores change as you select words.
What does it do?
It predicts a class, such as Spam or Not Spam, by comparing how likely the clues are in each class.
It is called “Bayes” because it uses Bayes’ theorem. It is called “naive” because it assumes clues are independent once the class is known.
How does it learn?
- Count how often each class appears.
- Learn how often each clue appears in each class.
- Use the counts to calculate probabilities.
- Choose the class with the highest score.
Build an email and predict its class
Select the words in a message. This small teaching dataset updates the class scores as you change your selection.
Choose some words to begin
The model will compare the two class scores.
Class scores
Choose a dataset to see its class balance.
Bayes’ theorem
Bayes’ theorem updates a probability after we see new information.
[P(Words | Class) × P(Class)] / P(Words)
In simple words: the chance of a class after seeing the words depends on how well the words fit that class and how common the class was before seeing them.
Why “naive”?
It treats each selected word as if it gives separate information once the class is known.
P(word₁ | Class) × P(word₂ | Class) × ...
This is not always true in real language, but it makes the model simple and fast.
Important terms
| Term | Meaning |
|---|---|
| Prior | How common a class is before checking clues. |
| Likelihood | How likely a clue is inside a particular class. |
| Posterior | The updated probability after seeing clues. |
| Laplace smoothing | Adds a small count so an unseen word does not make the whole score zero. |
Worked numerical example
Imagine 10 training emails: 4 are spam and 6 are normal. “Free” appears in 3 spam emails and 1 normal email. Use Laplace smoothing with two possible outcomes: word present or absent.
| Quantity | Spam | Not spam |
|---|---|---|
| Emails in class | 4 | 6 |
| Emails containing “free” | 3 | 1 |
| Prior | 4/10 = 0.40 | 6/10 = 0.60 |
| Smoothed likelihood | (3+1)/(4+2) = 0.667 | (1+1)/(6+2) = 0.25 |
Unscaled scores:
Not spam: 0.60 × 0.25 = 0.1500
The spam score is larger, so this example is classified as Spam. A real model compares all words and can normalise the scores so they add up to 1.
How do we test it?
Use data the model did not train on. Compare its predictions with the correct answers.
- Accuracy: how many predictions were correct.
- Precision: how many predicted positives were truly positive.
- Recall: how many real positives were found.
- F1-score: a balance of precision and recall.
Strengths and limits
Strengths
- Fast and simple.
- Often useful for text features.
- Can work with many features.
Limits
- Its independence assumption is often unrealistic.
- Probability estimates may not be perfectly calibrated.
- Results depend on useful features and data.
Three common types
| Type | Best suited for | Example |
|---|---|---|
| Multinomial | Counts, such as word counts | Email or news text |
| Bernoulli | Binary features: present or absent | Whether a word appears |
| Gaussian | Continuous number features | Measurements such as height |
Python implementation
This example uses Multinomial Naive Bayes with scikit-learn.
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
emails = [
"win a free prize now",
"free offer claim your prize",
"team meeting about the project",
"please review the project report",
"win a special offer",
"meeting schedule for tomorrow"
]
labels = ["spam", "spam", "normal", "normal", "spam", "normal"]
model = make_pipeline(CountVectorizer(), MultinomialNB())
model.fit(emails, labels)
new_email = ["free prize offer"]
print("Prediction:", model.predict(new_email)[0])
print("Probabilities:", model.predict_proba(new_email))Install scikit-learn first if needed: pip install scikit-learn.
Where is Naive Bayes used?
Email filtering
Spotting likely spam messages.
Sentiment analysis
Sorting reviews into positive or negative groups.
News sorting
Grouping articles by topic.
Document tagging
Finding likely categories for text.
Test what you learned
Answer all 10 questions, then submit to see your score and explanations.