🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
Search courses…
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

Naive Bayes — Complete Guide for Classification

ML FoundationsClassification🟢 Free Lesson

Advertisement

Supervised Learning

Bayes' Theorem in Action — Fast, Simple, Surprisingly Powerful

Naive Bayes applies Bayes' theorem with a "naive" independence assumption. Despite its simplicity, it often outperforms more complex algorithms on text classification.

  • Bayes' Theorem — The probabilistic foundation of classification
  • Conditional Independence — The simplifying assumption that makes it tractable
  • Gaussian / Multinomial / Bernoulli — Three variants for different data types

"Simplicity is the ultimate sophistication." — Leonardo da Vinci

Naive Bayes — Complete Guide

Naive Bayes applies Bayes' theorem with the "naive" assumption that features are conditionally independent given the class. Despite this simplification, it works remarkably well in practice.


Bayes' Theorem

Bayes' Theorem: From Prior to PosteriorPriorP(C_k)×LikelihoodP(x | C_k)PosteriorP(C_k | x)Naive Assumption: P(x₁,x₂,...,xₙ | C) = ∏ᵢ P(xᵢ | C)Features are conditionally INDEPENDENT given the classP(C_k | x) ∝ P(C_k) · ∏ᵢ P(xᵢ | C_k)Predict: argmax_k P(C_k) · ∏ᵢ P(xᵢ | C_k)

The Naive Independence Assumption

Joint vs Naive Bayes Parameter CountJoint Distribution P(x₁,...,xₙ|C)For n binary features, K classes:Parameters: K × 2ⁿExample: n=30 features, K=2 classesK × 2ⁿ = 2 × 1,073,741,824 ≈ 2 billion!Impossible to estimate with realistic dataNaive Bayes: ∏ᵢ P(xᵢ|C)Features assumed independent:Parameters: K × nExample: n=30 features, K=2 classesK × n = 2 × 30 = 60 parameters!Tractable — easily estimated from data

Variants

Gaussian Naive Bayes

Multinomial Naive Bayes

Bernoulli Naive Bayes

Naive Bayes Variants: Which to Use?Gaussian NBContinuous featuresAssumes normal distributionUse for: Iris, housing dataP(xᵢ|C) = N(μ, σ²)sklearn: GaussianNB()Multinomial NBCount/frequency featuresBest for text classificationUse for: Spam, sentimentP(xᵢ|C) = (Nᵢₖ+α)/(Nₖ+αn)sklearn: MultinomialNB()Bernoulli NBBinary features (0/1)Presence/absence onlyUse for: Binary text dataP(xᵢ=1|C) = count/Csklearn: BernoulliNB()

Text Classification Example

from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.model_selection import cross_val_score

emails = ["Win free money now", "Claim your prize", "Meeting tomorrow at 10",
          "Project update", "Buy discount pills", "Special offer just for you",
          "Team lunch Friday", "Quarterly report attached"]
labels = [1, 1, 0, 0, 1, 1, 0, 0]  # 1=spam, 0=not spam

vectorizer = CountVectorizer()
X = vectorizer.fit_transform(emails)

model = MultinomialNB(alpha=1.0)  # Laplace smoothing
model.fit(X, labels)

# Predict on new email
new_emails = ["Free prize waiting for you"]
X_new = vectorizer.transform(new_emails)
print(f"Prediction: {model.predict(X_new)[0]}")  # 1 (spam)
print(f"P(spam|words) = {model.predict_proba(X_new)[0][1]:.4f}")

When to Use Naive Bayes


Key Takeaways


What to Learn Next

-> Logistic Regression Classification with probability — from linear to sigmoid.

-> SVM Finding the optimal boundary — maximum margin classification.

-> NLP Fundamentals Natural language processing — tokenization, embeddings, and transformers.

Need Expert Machine Learning Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement