Naïve Bayes Classifier

 

Naïve Bayes Classifier 

🔍 What is Naïve Bayes?

The Naïve Bayes classifier is a supervised machine learning algorithm used for classification tasks. It is based on Bayes’ Theorem and predicts the class of a data point using probabilities.

👉 It is called:

  • “Bayes” → because it is based on Bayes’ theorem
  • “Naïve” → because it assumes independence between features

This means:

The presence of one feature does not affect the presence of another feature.


🧠 Core Idea

In classification, we want to compute:

P(hd)P(h \mid d)

👉 Probability of class hh given data dd

Using Bayes’ theorem:

P(hd)=P(dh)P(h)P(d)P(h|d) = \frac{P(d|h)\cdot P(h)}{P(d)}

🎯 Goal: Choose Best Class (MAP)

We compute probabilities for all classes and choose the maximum:

MAP(h)=argmaxP(dh)P(h)MAP(h) = \arg\max P(d|h)\cdot P(h)

👉 This is called Maximum A Posteriori (MAP)

  • We ignore P(d)P(d) because it is constant for all classes

⚙️ Why “Naïve”? (Key Assumption)

Instead of computing:

P(d1,d2,d3h)P(d_1, d_2, d_3 | h)

Naïve Bayes assumes:

P(dh)=P(d1h)P(d2h)P(d3h)P(d|h) = P(d_1|h)\cdot P(d_2|h)\cdot P(d_3|h)\cdots

👉 This simplifies computation drastically

📌 Important:

  • This assumption is rarely true in real life
  • But the algorithm still performs surprisingly well

📊 How Naïve Bayes Works (Step-by-Step)

1. 🧾 Calculate Class Probabilities (Prior)

P(class)=count of classtotal instancesP(class) = \frac{\text{count of class}}{\text{total instances}}

Example:

  • If 50 out of 100 emails are spam:

    P(spam)=0.5P(spam) = 0.5


2. 🔗 Calculate Conditional Probabilities (Likelihood)

For each feature:

P(featureclass)P(feature \mid class)

Example:

P(money|
class=spam)
P(money \mid class = spam)

👉 Calculated as:

count(feature AND class)count(class)\frac{\text{count(feature AND class)}}{\text{count(class)}}


3. 🧮 Compute Posterior for Each Class

P(classdata)P(dataclass)P(class)P(class \mid data) \propto P(data \mid class)\cdot P(class)

4. 🏆 Choose Class with Maximum Probability

👉 Highest value = predicted class


📧 Example: Spam Detection

We want:



P(spam \mid money)

P(spammoney)=P(moneyspam)P(spam)P(money)= \frac{P(money \mid spam)\cdot P(spam)}{P(money)}

Interpretation:

  • P(spam)P(spam) → Prior (how common spam is)
  • P(moneyspam) → Likelihood (word “money” in spam)
  • P(money)P(money)→ Evidence
  • P(spammoney) → Final probability

👉 If:

P(spammoney)>P(not_spammoney)P(spam|money) > P(not\_spam|money)

➡️ classify as spam


⚡ Advantages

  • ✅ Simple and easy to implement
  • ✅ Very fast (good for real-time prediction)
  • ✅ Works well for multi-class problems
  • ✅ Excellent for text classification

Examples:

  • Spam filtering
  • Sentiment analysis
  • Document classification

⚠️ Disadvantages

  • ❌ Assumes feature independence (not realistic)
  • ❌ Cannot capture relationships between features

📌 Types of Naïve Bayes

1. Gaussian Naïve Bayes

  • For continuous data
  • Assumes normal distribution

2. Multinomial Naïve Bayes

  • For text data (word counts)
  • Used in NLP tasks

3. Bernoulli Naïve Bayes

  • For binary features
  • Example: word present or not

🌍 Applications

  • 📧 Spam detection
  • 😊 Sentiment analysis
  • 🏥 Medical diagnosis
  • 💳 Credit scoring
  • 🎯 Recommendation systems

💡 Final Insight

You can summarize Naïve Bayes as:    

“Calculate probability of each class using Bayes’ theorem, assume features are independent, and pick the most probable class.”

Comments

Popular posts from this blog

Machine Learning PCCST503 Semester5 KTU CS 2024 Scheme - Dr Binu V P

Introduction to Machine Learning (ML)

Distinguishing Machine Learning from Traditional Programming