Skip to main content
LearnMLAcademy
Intermediate10 min read

Naive Bayes

Apply Bayes theorem with conditional independence assumptions for text, with Python code, architecture diagrams, worked examples, and interview quiz.

What you will master in this lesson
Intuitive core mechanisms & practical real-world intuition
Step-by-step mathematical breakdown & visual diagrams
Runnable Python implementation & practical trade-offs

Naive Bayes Classifier

Naive Bayes is a probabilistic classifier based on applying Bayes’ Theorem with a strong (naive) conditional-independence assumption between features given the class.

Naive Bayes in Simple Words

Naive Bayes looks at clues, checks how often those clues appeared with each class in the past, and chooses the class with the strongest probability score. A simple example is email spam detection.

1

Past Emails

Learn from examples labelled Spam or Not Spam.

2

Look at Clues

Words such as free, prize, meeting or report.

3

Compare Scores

How strongly do the clues support each class?

4

Choose a Class

Predict the class with the larger score.

Tiny Example — One New Email

New email contains

free · prize · click

Past data suggests

These words occur more often in spam.

Prediction

Spam

The model does not understand the meaning of “free” like a human does. It uses probabilities learned from labelled examples.

What is Naive Bayes?

Naive Bayes is a supervised machine learning algorithm used mainly for classification problems. It predicts the probability that a data point belongs to a particular class based on Bayes' Theorem combined with a Feature Independence Assumption.

Common Uses:

  • Spam Detection: Filtering out junk emails.
  • Sentiment Analysis: Understanding positive/negative product reviews.
  • Document Classification: Organizing news articles into categories.
  • Medical Data Classification: Supporting carefully validated research or decision-support tasks.

* Naive Bayes is especially famous in Natural Language Processing (NLP) because it works efficiently with high-dimensional text data.


Simple Example: Spam Detection

Suppose an email contains words like: FREE, WINNER, MONEY, CLICK

Naive Bayes calculates two things:

  • 1. Probability (Spam | Email)
  • 2. Probability (Not Spam | Email)

It then predicts the class with the higher probability.

Why is it Called "Naive"?

The “naive” assumption is more specific than saying that features are always unrelated. Naive Bayes treats the features as conditionally independent once the class is known.

Spam example: suppose an email contains both FREE and MONEY.

When scoring the class Spam, the simple Naive Bayes idea is:

P(FREE and MONEY | Spam) ≈ P(FREE | Spam) × P(MONEY | Spam)

In real language, words can clearly be related. The assumption is therefore a simplification, not a statement that the real world is independent. Even so, this approximation can work well for many classification tasks, especially text.


Bayes’ Theorem Formulas

Naive Bayes is built directly on Bayes’ Theorem. Bayes’ Theorem helps calculate conditional probability — the probability of an event given another event has occurred.

Bayes' Formula

P(A | B) = [ P(B | A) × P(A) ] / P(B)

Posterior = (Likelihood × Prior) / Evidence

Terminology

  • P(A|B) Posterior: Updated probability of class A after observing evidence B.
  • P(B|A) Likelihood: Probability of observing evidence B when class A is true.
  • P(A) Prior: Probability of class A before observing the new evidence.
  • P(B) Evidence: Overall probability of observing evidence B.

A Tiny Bayes Calculation

Suppose past email data tells us:

Given valueMeaning
P(Spam) = 0.4040% of emails are spam.
P(Free | Spam) = 0.7575% of spam emails contain “free”.
P(Free) = 0.3636% of all emails contain “free”.

Step 1 — Multiply

0.75 × 0.40 = 0.30

Likelihood × prior

Step 2 — Divide

0.30 ÷ 0.36 ≈ 0.833

Divide by the evidence

Result

P(Spam | Free) ≈ 83.3%

For these made-up teaching numbers.

Real-Life Analogy: A common probability example is medical testing: if a condition is rare but a test is positive, Bayes’ theorem updates the probability after observing that result. In real healthcare, diagnosis requires validated clinical evidence and cannot be reduced to one toy calculation.


Worked Numerical Example

Let's predict whether a day is suitable for playing golf based on the weather outlook.

Outlook (Feature)Play Golf (Target)
SunnyYes
RainyNo
SunnyYes
OvercastYes
RainyNo

Step-by-Step Probability Calculation

  1. Step 1: Calculate Priors

    Total rows = 5

    P(Yes) = 3/5 = 0.6

    P(No) = 2/5 = 0.4

  2. Step 2: Calculate Likelihoods

    Looking at "Sunny":

    P(Sunny | Yes) = 2/3 ≈ 0.666

  3. Step 3: Calculate the Evidence

    Sunny appears 2 times out of 5 rows.

    P(Sunny) = 2/5 = 0.4

  4. Step 4: Calculate the Posterior

    P(Yes | Sunny) = [(2/3) × (3/5)] ÷ (2/5)

    = 0.4 ÷ 0.4 = 1.0

    In this tiny dataset, every Sunny example is labelled Yes, so the posterior becomes 100%. Real datasets are larger and less perfect.


Types of Naive Bayes

Naive Bayes is not a single algorithm — it is a family of classifiers, each designed for a different statistical distribution of features. The appropriate variant depends mainly on how the input features are represented and what probability distribution the model assumes for them.

Choose the variant from the feature type

Gaussian NB

Continuous numbers

Height, temperature

Multinomial NB

Counts / frequencies

Word counts

Bernoulli NB

Binary 0/1 features

Word present?

Complement NB

Count-like text features

Often useful with imbalance

Categorical NB

Categorical features

Weather type, colour

1. Gaussian Naive Bayes

For continuous, real-valued features

Gaussian NB is used when your features are continuous numerical values — things like temperature, salary, height, or age. It assumes that within each class, the values of each feature follow a normal (Gaussian / bell-curve) distribution. During training, it estimates the mean (μ) and variance (σ²) of each feature for each class.

How it works:

  1. For each class, calculate the mean and variance of every feature
  2. For a new sample, compute how likely each feature value is under that class's Gaussian curve
  3. Multiply all likelihoods together (naïve assumption) and pick the class with highest probability

Real-world use cases:

  • Medical diagnosis (blood pressure, glucose levels)
  • Fraud detection (transaction amounts, timing)
  • Weather classification (temperature, humidity)
  • Iris flower species classification

Gaussian Distribution per class — each class has its own μ and σ

Feature Value (e.g. Sepal Length)Class 0μ=5.0, σ=0.8Class 1μ=6.5, σ=1.0
Python — Gaussian Naive Bayes
from sklearn.naive_bayes import GaussianNB
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load dataset (continuous features: sepal/petal length & width)
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train Gaussian NB — automatically estimates μ and σ per class per feature
model = GaussianNB()
model.fit(X_train, y_train)

# Class-conditional means learned during training:
print(model.theta_)   # shape: [n_classes, n_features] → mean per class
print(model.var_)     # shape: [n_classes, n_features] → variance per class

predictions = model.predict(X_test)
print(f"Accuracy: {accuracy_score(y_test, predictions):.2%}")
# Output with this split: Accuracy: 100.00%

2. Multinomial Naive Bayes

For count data — the NLP workhorse

Multinomial NB works with discrete count or frequency data — most commonly word counts in text classification tasks. It models the probability of observing a particular word count given the class. For example: if the word "free" appears 50 times in spam emails and only 3 times in legitimate emails, Multinomial NB captures this frequency ratio as evidence.

How it works:

  1. Convert text into a word frequency matrix (Bag of Words / TF-IDF)
  2. For each class, estimate the probability of each word appearing
  3. For new text, combine the word evidence with the class prior and choose the class with the larger score

Real-world use cases:

  • Email spam classification (most common use)
  • Sentiment analysis of reviews
  • News article categorisation
  • Language identification

Example — Word frequency evidence for spam classification

WordSpam freqHam freqMore common in
"free"1428Spam
"money"9812Spam
"click"875Spam
"meeting"4156Ham
"report"6134Ham

3. Bernoulli Naive Bayes

For binary presence/absence features

Bernoulli NB is designed for binary feature vectors — each feature is either 0 (absent) or 1 (present). The key distinction from Multinomial NB is that Bernoulli NB cares only about whether a word appeared, not how many times it did. It also explicitly penalises the absence of words that are important to a class, which can make it useful for presence/absence data and some short-text tasks.

Multinomial vs Bernoulli — key difference:

AspectMultinomialBernoulli
Feature valueWord count (0,1,2,...)Word present? (0 or 1)
Email "free free free"free = 3free = 1
Absent word impactNo penaltyExplicitly penalised
Typical representationCounts / frequenciesPresence / absence

Use cases:

  • Short text / tweet classification
  • Document presence/absence features
  • Medical symptom checkers (symptom present or not)
  • Click-stream data (did user click? Y/N)
Python — Bernoulli Naive Bayes
from sklearn.naive_bayes import BernoulliNB
from sklearn.feature_extraction.text import CountVectorizer
import numpy as np

emails = [
    "win free money now",      # spam
    "click here for prize",    # spam
    "meeting tomorrow at 10",  # ham
    "quarterly report attached", # ham
]
labels = [1, 1, 0, 0]  # 1=spam, 0=ham

# binary=True converts word counts to 0/1 presence flags
vectorizer = CountVectorizer(binary=True)
X = vectorizer.fit_transform(emails)
# Each row: [0,1,0,0,1,...] — word present (1) or absent (0)

model = BernoulliNB()
model.fit(X, labels)

new_email = vectorizer.transform(["free prize click"])
print(model.predict(new_email))         # [1] → classified as SPAM
print(np.round(model.predict_proba(new_email), 3))
# [[0.053 0.947]] → about 94.7% spam in this tiny example

4. Complement Naive Bayes

For imbalanced datasets — a Multinomial NB improvement

Complement NB is an adaptation of Multinomial NB that is particularly suited to imbalanced datasets. It builds its weights using statistics from the complement of each class — the samples that are not in that class. It is especially associated with text classification and can be worth comparing with Multinomial NB when classes are uneven.

Why Complement? The imbalance problem:

If 90% of the training documents are "Not Spam" and 10% are "Spam", the class counts are strongly imbalanced. Complement NB uses statistics from the other classes when building its weights, which can make its estimates more stable for this kind of text problem. You should still compare models using suitable validation metrics.

Python — Complement NB (imbalanced dataset)
from sklearn.naive_bayes import ComplementNB
from sklearn.feature_extraction.text import TfidfVectorizer

# Complement NB is a drop-in replacement for MultinomialNB
# Best for: imbalanced text classification tasks
vectorizer = TfidfVectorizer()
X_train_tfidf = vectorizer.fit_transform(X_train_text)
X_test_tfidf  = vectorizer.transform(X_test_text)

model = ComplementNB(alpha=1.0)  # alpha = Laplace smoothing
model.fit(X_train_tfidf, y_train)
print(f"Accuracy: {model.score(X_test_tfidf, y_test):.2%}")

5. Categorical Naive Bayes

For features that are categories

Categorical NB is designed for features such as Weather = Sunny/Rainy or Colour = Red/Blue/Green. Each feature is treated as having its own categorical distribution within each class. In scikit-learn, the categories are represented with integer codes such as 0, 1, 2.

Sunny
→ 0
Rainy
→ 1
Overcast
→ 2

These numbers are category codes, not measurements — “2” is not twice “1”.

Quick-Reference: Which Naive Bayes to Use?

VariantFeature TypeBest Forsklearn Class
Gaussian NBContinuous (real numbers)Continuous numeric featuresGaussianNB()
Multinomial NBCounts / non-negative frequenciesText counts; TF-IDF can also workMultinomialNB()
Bernoulli NBBinary (0/1)Presence / absence featuresBernoulliNB()
Complement NBCount / TF-IDF-like non-negative featuresText data, especially with imbalanceComplementNB()
Categorical NBEncoded categorical valuesDiscrete categories per featureCategoricalNB()

The Zero Probability Problem

In count-based variants such as Multinomial Naive Bayes, a word that never appeared in a class can receive an estimated probability of 0. Multiplying by zero can wipe out that class score. Additive smoothing prevents this problem.

Solution: Additive Smoothing

Add a small value α to the counts. When α = 1, this is commonly called Laplace smoothing.

Tiny smoothing example

Suppose “offer” appeared 0 times in a class, there are 10 total word counts, and our vocabulary has 5 words.

Step 1
0 + 1 = 1
add α = 1
Step 2
10 + (1 × 5) = 15
adjust denominator
Result
1 ÷ 15 ≈ 0.067
small, but not zero

Python Implementations

Python Code: Multinomial NB (Text Data)

from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer

texts = ["win free money", "hello friend", "claim prize free"]
y = ["spam", "ham", "spam"]

# Convert text to frequency counts
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)

# Train Model
model = MultinomialNB()
model.fit(X, y)

# Predict a new email
test_email = ["free money now"]
test_vector = vectorizer.transform(test_email)
prediction = model.predict(test_vector)

print(f"Prediction for '{test_email[0]}': {prediction[0]}")
Terminal Output

$ python nb_classifier.py

Prediction for 'free money now': spam


Advantages vs Disadvantages

Advantages

  • Fast: Training and prediction can be very efficient compared with many more complex models.
  • Useful for Text: Count-based variants are classic baselines for document and spam classification.
  • Parameter Efficient: It can estimate the needed class-conditional statistics from relatively modest amounts of data.
  • Multi-Class: Naturally compares scores across multiple classes.

Disadvantages

  • Strong Assumption: Conditional independence can be unrealistic when features strongly depend on one another.
  • Unseen Counts: Count-based variants need additive smoothing to avoid zero-probability problems.
  • Probability Calibration: Predicted probabilities may be poorly calibrated even when class predictions are useful.

Common Questions

Is Naive Bayes only for text?

No. Text is a famous use case, but Gaussian NB can handle continuous numerical features and Categorical NB can model categorical features.

Does Naive Bayes require the features to be truly independent?

No real dataset has to be perfectly independent. Conditional independence is the model's simplifying assumption; validation tells you whether that approximation works well enough for your task.

Naive Bayes or Logistic Regression?

Both can be strong classification baselines. Compare them using validation data and a metric that matches the problem instead of assuming one is always better.

Final Summary

Naive Bayes is a classic probabilistic classification method built on Bayes' theorem. It combines a class prior with feature likelihoods to score possible classes for a new observation.

Its conditional-independence assumption is deliberately simple, yet Naive Bayes can still be a strong and very fast baseline in suitable problems, especially text classification.

Most Important Insight to Remember:

"Naive Bayes makes a strong conditional-independence assumption. The assumption is simple, so always validate the model — but the method can still work very well for suitable classification tasks."

Apply this concept in a real hands-on project

You have studied the idea. Now use it in working Python code, see the calculation and output, and explain what changed. Each project includes step-by-step instructions, full source and tests.

Practice challenge: predict an outcome first, run the code, and explain the difference.

Track Your Progress0% curriculum done

Finished "Naive Bayes"?

Mark this lesson as completed to update your progress bar and track your milestones across the full curriculum.

Overall Progress0 / 167 lessons

Was this tutorial helpful?

Your feedback helps us continuously refine and improve our open-access tutorials.

Summary / Key Takeaways

  • Naive Bayes combines class priors and feature likelihoods with Bayes' rule.
  • It assumes conditional feature independence given the class.
  • Gaussian, multinomial, and Bernoulli variants match different features.
  • Log probabilities prevent numerical underflow.
  • Smoothing prevents unseen values from forcing zero probability.
  • Useful predictions can occur despite imperfect assumptions, but calibration still needs checking.

Knowledge Check

Progress: 0/4

Test your active recall. Choose one answer for each question, then check it to reveal the step-by-step reasoning.

Question 1 of 4
What does naive mean in Naive Bayes, and why is this assumption made despite being wrong?
Question 2 of 4
Despite the independence assumption being violated, Naive Bayes works well for spam classification. Why?
Question 3 of 4
During training, the word cryptocurrency never appeared in spam. Without smoothing, what happens when the model sees a spam email containing it?
Question 4 of 4
You have 50,000 training examples, 200,000 vocabulary features, and need a model in 2 hours. Why is Naive Bayes an excellent starting point?

Knowledge Check Review & Explanations

Review the questions, verified correct answers, and conceptual explanations for this lesson.

1What does naive mean in Naive Bayes, and why is this assumption made despite being wrong?▼

Correct Answer: Naive Bayes assumes all features are conditionally independent given the class — almost always violated in real data. The assumption is made because it allows O(n×d) training instead of exponential training needed for a full joint distribution over all features

Without the independence assumption, learning P(all features|class) requires exponentially many parameters. The naive assumption makes the problem tractable: P(features|class) = ΠP(featureᵢ|class), one parameter per feature per class.

2Despite the independence assumption being violated, Naive Bayes works well for spam classification. Why?▼

Correct Answer: Even if probabilities are wrong in absolute terms, the relative ranking of class probabilities is often preserved. Classification only requires identifying the highest-probability class, not accurate probability values

Classification accuracy requires only P(spam|email) > P(not-spam|email), not calibrated probabilities. The independence assumption degrades calibration but often preserves the ordering, keeping accuracy high.

3During training, the word cryptocurrency never appeared in spam. Without smoothing, what happens when the model sees a spam email containing it?▼

Correct Answer: P(cryptocurrency|spam)=0, which makes P(email|spam)=0 no matter what other spam signals are present. The email can never be classified as spam regardless of containing 100 other spam words. Laplace smoothing adds 1 to every word count, ensuring no probability is ever exactly zero

This is the zero-frequency problem. One unseen word multiplies the entire class probability by zero. Laplace smoothing: add 1 to every word count so every word always has a nonzero probability.

4You have 50,000 training examples, 200,000 vocabulary features, and need a model in 2 hours. Why is Naive Bayes an excellent starting point?▼

Correct Answer: Naive Bayes trains in O(n×d) time — scanning training examples once to count word frequencies per class. For 50,000 documents and 200,000 vocabulary terms, training is nearly instant. It provides a strong baseline in minutes, benchmarked against which you can justify spending time on more expensive models

Naive Bayes is the standard first baseline for text classification. It trains in seconds on datasets that would take hours for gradient boosting or BERT fine-tuning.

Keep moving through the path

Continue Learning

Free Download: ML Interview Cheatsheet

100 essential Machine Learning interview questions and answers covering foundations, algorithms, evaluation, deep learning, Python, system design and interview strategy. Enter your email to download it instantly.

Optional Learning Resources

These resources may complement the lesson. They are optional and are not required to use ML Academy.

As an Amazon Associate I earn from qualifying purchases. Other links are identified separately when they are not affiliate links.

Next