Long before modern AI, email providers were catching spam with a surprisingly simple idea: count the words, apply a 250-year-old probability rule, and pick the most likely answer. That idea is Naive Bayes — still one of the fastest and most practical classifiers, especially for text. This guide explains what Naive Bayes is, how it works, why and when to use it, with a worked example and Python code.
Table of Contents
- What is Naive Bayes?
- Bayes’ theorem in plain words
- Why is it called “naive”?
- How Naive Bayes works (step by step)
- Worked example: spam or not?
- Types of Naive Bayes
- Laplace smoothing & log probabilities
- Why use Naive Bayes?
- Real-world applications
- Python examples
- Advantages and limitations
- Naive Bayes vs other algorithms
- How to learn it
- FAQs
What Is Naive Bayes?
Naive Bayes is a family of supervised machine learning classifiers based on Bayes’ theorem. For a new example, it calculates the probability of each class given the features — for instance, P(spam | message) and P(not spam | message) — and predicts the class with the highest probability.
It is called a probabilistic and generative classifier: it learns how features are distributed within each class, then uses those patterns to reason backwards from the features to the most likely class.
Bayes’ Theorem in Plain Words
P(class | features) = P(features | class) × P(class) ÷ P(features)
| Term | Name | Meaning in a spam filter |
|---|---|---|
| P(class | features) | Posterior | Probability the message is spam, given its words — what we want |
| P(features | class) | Likelihood | How often these words appear in spam messages |
| P(class) | Prior | How common spam is overall |
| P(features) | Evidence | How common these words are overall — the same for every class, so it can be ignored when comparing |
In words: update your initial belief (prior) using the evidence (likelihood) to get an updated belief (posterior).
Why Is It Called “Naive”?
Calculating the probability of an exact combination of hundreds of words is impossible with real data. Naive Bayes makes a simplifying — “naive” — assumption: every feature is independent of the others, given the class. That lets it multiply individual probabilities:
P(word₁, word₂, … | spam) ≈ P(word₁ | spam) × P(word₂ | spam) × …
The assumption is rarely true (“free” and “offer” often appear together), yet the classifier still works well in practice, because it only needs to rank the classes correctly, not to produce perfect probabilities.
How Naive Bayes Works (Step by Step)
- Training — learn priors: the share of each class in the training data.
- Training — learn likelihoods: for each feature and class, how likely each value is (word frequencies for text; mean and variance for numbers).
- Prediction: for a new example, multiply the prior by the likelihood of each of its features, for every class.
- Compare: the class with the highest score wins. Normalising the scores gives probabilities.
Training is just counting and averaging — one pass through the data — which is why Naive Bayes is so fast.
Worked Example: Spam or Not?
From past emails, a model has learned these probabilities (illustrative numbers):
| Spam | Not spam (ham) | |
|---|---|---|
| Prior P(class) | 0.40 | 0.60 |
| P(“free” | class) | 0.60 | 0.05 |
| P(“offer” | class) | 0.50 | 0.10 |
New email: contains the words “free” and “offer”.
- Spam score = 0.40 × 0.60 × 0.50 = 0.120
- Ham score = 0.60 × 0.05 × 0.10 = 0.003
- Normalise: P(spam | email) = 0.120 ÷ (0.120 + 0.003) ≈ 0.976 → about 98% spam
Even though spam is less common overall (40% prior), the words are so much more typical of spam that the posterior swings strongly towards spam.
Types of Naive Bayes
| Type | Feature type | Typical use | scikit-learn class |
|---|---|---|---|
| Multinomial NB | Counts (e.g. word counts, TF-IDF) | Text classification, spam, topics | MultinomialNB |
| Bernoulli NB | Binary yes/no features (word present or not) | Short texts, binary indicators | BernoulliNB |
| Gaussian NB | Continuous numbers, assumed normally distributed | Sensor or measurement data | GaussianNB |
| Complement NB | Counts, adapted for imbalanced classes | Imbalanced text datasets | ComplementNB |
| Categorical NB | Categorical features | Survey or form data with categories | CategoricalNB |
Laplace Smoothing & Log Probabilities
- The zero-frequency problem: if a word never appeared in spam during training, P(word | spam) = 0, and multiplying by 0 wipes out the entire spam score. Laplace (add-one) smoothing adds a small count to every word so no probability is zero. In scikit-learn this is the
alphaparameter (default 1.0). - Underflow: multiplying hundreds of small probabilities produces numbers too tiny for a computer. Implementations add log probabilities instead of multiplying probabilities — same result, numerically safe.
Why Use Naive Bayes?
- Extremely fast to train and predict — even on millions of documents.
- Works with little training data, because it estimates only simple per-feature statistics.
- Excellent for text — a strong, hard-to-beat baseline for spam, sentiment and topic classification.
- Handles many features (thousands of words) without trouble.
- Gives probabilities and naturally handles multiple classes.
- Easy to update with new data (
partial_fit) — useful for streaming data. - Interpretable — you can list the words most associated with each class.
Real-World Applications
| Area | Use of Naive Bayes |
|---|---|
| Email & SMS | Spam and phishing filtering |
| Customer feedback | Sentiment analysis of reviews (positive / negative) |
| News & content | Topic categorisation of articles and support tickets |
| Language detection | Identifying the language of a text from character patterns |
| Recommendations | Simple “will this user like it?” probability models |
| Healthcare | Quick probabilistic screening from symptoms |
| Real-time systems | Classifying streaming text where speed matters |
Text classification is a core part of NLP, where Naive Bayes is usually the first model students build.
Python Examples
Example 1: A spam filter (Multinomial NB)
This example trains on a tiny sample of 10 messages so it runs anywhere. In a real project, you would train on thousands of labelled messages.
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
messages = [
"Win a free iPhone now, click the link",
"Congratulations! You won a cash prize, claim free offer",
"Limited offer: get free recharge today",
"Urgent: your account will be blocked, click to verify",
"Free entry in our lucky draw, reply WIN",
"Are we meeting at 5 pm for the project?",
"Please send the class notes from today",
"Can you call me when you reach Jaipur?",
"The team meeting is moved to Monday",
"Thanks for the help with the assignment",
]
labels = ["spam"] * 5 + ["ham"] * 5
# Turn text into word counts, then fit Naive Bayes (alpha = Laplace smoothing)
spam_filter = make_pipeline(CountVectorizer(lowercase=True), MultinomialNB(alpha=1.0))
spam_filter.fit(messages, labels)
tests = ["Claim your free prize now", "Meeting notes for Monday class"]
spam_col = list(spam_filter.classes_).index("spam")
for msg, label, probs in zip(tests, spam_filter.predict(tests), spam_filter.predict_proba(tests)):
print(f"{msg!r} -> {label} (P(spam) = {probs[spam_col]:.2f})")Output: “Claim your free prize now” → spam (P(spam) = 0.99); “Meeting notes for Monday class” → ham (P(spam) = 0.01).
Example 2: Numeric features (Gaussian NB)
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.naive_bayes import GaussianNB
X, y = load_wine(return_X_y=True) # 178 wines, 13 numeric features, 3 classes
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42, stratify=y)
gnb = GaussianNB()
print("CV accuracy :", round(cross_val_score(gnb, X_train, y_train, cv=5).mean(), 3))
print("Test accuracy:", round(gnb.fit(X_train, y_train).score(X_test, y_test), 3))Output: cross-validation accuracy 0.955; test accuracy 0.978 — with no tuning and almost no training time. (Scaling is not required for Naive Bayes.)
Advantages and Limitations
| Advantages | Limitations |
|---|---|
| Very fast training and prediction | Independence assumption is usually violated |
| Needs little data | Predicted probabilities are often over-confident — calibrate if you need accurate probabilities |
| Great baseline for text | Usually less accurate than tuned SVM, boosting or transformers on complex tasks |
| Handles many features and classes | Gaussian NB assumes normally distributed numeric features |
| No feature scaling needed | Zero-frequency problem without smoothing |
| Easy to interpret and update | Correlated or duplicated features get “double-counted” |
Naive Bayes vs Other Algorithms
| Naive Bayes | Logistic regression | SVM | KNN | |
|---|---|---|---|---|
| Model type | Generative (probabilistic) | Discriminative | Discriminative (max margin) | Instance-based |
| Training speed | Fastest | Fast | Slower | None |
| Small data | Very good | Good | Good | Fair |
| Text classification | Strong baseline | Strong | Often strongest classical model | Weak |
| Probabilities | Yes (often over-confident) | Yes (well calibrated) | Optional | Vote share |
Related reads: logistic regression, SVM and KNN.
How to Learn Naive Bayes
- Revise basic probability: conditional probability and Bayes’ theorem
- Work through the spam example above by hand
- Learn text vectorisation: bag of words, CountVectorizer, TF-IDF
- Build a spam or sentiment classifier with MultinomialNB
- Compare it against logistic regression and linear SVM on the same data
- Try GaussianNB on a numeric dataset and see where the independence assumption hurts
Reference: the scikit-learn Naive Bayes guide.
Learn it in Jaipur: classic ML classification and text preprocessing are part of our AI & Machine Learning course, including its NLP module, and our Data Science course applies ML to real datasets. 6 months, 2-hour practical classes, IBM certificate on completion — in Jhotwara or live online.
Frequently Asked Questions
Is Naive Bayes supervised or unsupervised?
Supervised — it learns from labelled examples.
Why does Naive Bayes work if its assumption is wrong?
Classification only needs the correct class to have the highest score. Even when the probabilities are distorted by correlated features, the ranking is often still right.
Which Naive Bayes should I use for text?
Start with Multinomial NB on word counts or TF-IDF; try Complement NB for imbalanced classes and Bernoulli NB for very short texts.
Does Naive Bayes need feature scaling?
No. It works with per-feature probabilities, so scaling does not change the results.
Is Naive Bayes still used today?
Yes — as a fast baseline, for real-time and low-resource text classification, and wherever training data is limited. For the highest accuracy on complex language tasks, transformer models are now preferred.
Learn machine learning hands-on in Jaipur
Explore our AI & Machine Learning, Data Science and Predictive AI courses. Send an enquiry or WhatsApp us on +91 82096 15423. Also read: What Is SVM? and What Is NLP?
