Naive Bayes Classifier - Example Problems
Example1: Spam Detection using Naรฏve Bayes
๐งพ Step 1: Training Dataset
| Email ID | Contains “offer” | Contains “money” | Class |
|---|---|---|---|
| 1 | Yes | Yes | Spam |
| 2 | Yes | No | Spam |
| 3 | No | Yes | Spam |
| 4 | Yes | Yes | Spam |
| 5 | No | No | Not Spam |
| 6 | No | Yes | Not Spam |
| 7 | Yes | No | Not Spam |
| 8 | No | No | Not Spam |
๐ Step 2: Compute Prior Probabilities
Total emails = 8
- Spam = 4
- Not Spam = 4
๐ Step 3: Compute Likelihoods
For Spam
For Not Spam
๐ฏ Step 4: New Email Prediction
๐ New email contains:
- “offer” = Yes
- “money” = Yes
We classify this email.
๐ข Apply Naรฏve Bayes
Formula
๐งฎ Compute for Spam
๐งฎ Compute for Not Spam
๐ Final Decision
- Spam score = 0.28125
- Not Spam score = 0.03125
๐ Prediction: SPAM
Example2: Problem: Should We Play?
We want to predict:
If Weather = Sunny, should we Play = Yes or No?
๐ Step 1: Sample Dataset
| Day | Weather | Play |
|---|---|---|
| 1 | Sunny | No |
| 2 | Sunny | No |
| 3 | Overcast | Yes |
| 4 | Rainy | Yes |
| 5 | Rainy | Yes |
| 6 | Rainy | No |
| 7 | Overcast | Yes |
| 8 | Sunny | No |
| 9 | Sunny | Yes |
| 10 | Rainy | Yes |
| 11 | Sunny | Yes |
| 12 | Overcast | Yes |
| 13 | Overcast | Yes |
| 14 | Rainy | No |
๐ Step 2: Frequency Table
๐ฏ Class Counts
- Yes = 9
- No = 5
- Total = 14
๐ค️ Weather vs Play
For Play = Yes (9)
| Weather | Count |
|---|---|
| Sunny | 2 |
| Overcast | 4 |
| Rainy | 3 |
For Play = No (5)
| Weather | Count |
|---|---|
| Sunny | 3 |
| Overcast | 0 |
| Rainy | 2 |
๐ Step 3: Likelihood Probabilities
For Play = Yes
For Play = No
๐ Step 4: Prior Probabilities
๐ข Step 5: Apply Bayes’ Theorem
Since
๐งฎ Compute for Play = Yes
๐งฎ Compute for Play = No
๐ Final Decision
- Yes → 0.1429
- No → 0.2143
๐ Prediction: NO (Do not play)
๐ Problem: Accident Prediction Using Naรฏve Bayes
We want to predict:
Will an accident occur (Yes/No) based on:
- Weather Condition
- Road Condition
- Traffic Condition
- Engine Problem
๐ Step 1: Sample Dataset
| ID | Weather | Road | Traffic | Engine | Accident |
|---|---|---|---|---|---|
| 1 | Sunny | Good | Low | No | No |
| 2 | Rainy | Poor | High | Yes | Yes |
| 3 | Foggy | Poor | High | No | Yes |
| 4 | Sunny | Good | Medium | No | No |
| 5 | Rainy | Poor | High | Yes | Yes |
| 6 | Sunny | Poor | Medium | Yes | Yes |
| 7 | Foggy | Good | Low | No | No |
| 8 | Rainy | Good | Medium | No | No |
| 9 | Sunny | Poor | High | Yes | Yes |
| 10 | Foggy | Poor | High | Yes | Yes |
๐ฏ Problem Statement for Students
๐ Given a new condition:
- Weather = Rainy
- Road = Poor
- Traffic = High
- Engine = Yes
Question:
Will an accident occur?
๐ Step 2: Frequency (Class Counts)
- Accident = Yes → 6
- Accident = No → 4
๐ Step 3: Prior Probabilities
๐ Step 4: Likelihood Probabilities
For Accident = Yes (6 cases)
For Accident = No (4 cases)
๐ข Step 5: Apply Naรฏve Bayes
Formula:
๐งฎ Without Smoothing (for understanding)
For Yes
๐ Non-zero → valid
For No
๐ Final Decision
๐ Since:
- Yes → non-zero probability
- No → 0
➡️ Prediction: Accident = YES
๐ Problem: Student Performance Prediction
We want to predict:
Will a student Pass or Fail?
Based on:
- Study Hours (High / Low)
- Attendance (Good / Poor)
- Assignment Submission (Yes / No)
๐ Step 1: Sample Dataset
| ID | Study | Attendance | Assignment | Result |
|---|---|---|---|---|
| 1 | High | Good | Yes | Pass |
| 2 | High | Good | Yes | Pass |
| 3 | High | Poor | Yes | Pass |
| 4 | Low | Good | Yes | Pass |
| 5 | Low | Poor | No | Fail |
| 6 | Low | Poor | No | Fail |
| 7 | High | Good | No | Pass |
| 8 | Low | Good | No | Fail |
| 9 | High | Poor | No | Fail |
| 10 | Low | Good | Yes | Pass |
๐ฏ Problem Statement
๐ Given a new student:
- Study = High
- Attendance = Good
- Assignment = No
Question:
Will the student Pass or Fail?
๐ Step 2: Class Counts
- Pass = 6
- Fail = 4
- Total = 10
๐ Step 3: Prior Probabilities
๐ Step 4: Likelihood Probabilities
For Pass (6 cases)
For Fail (4 cases)
๐ข Step 5: Apply Naรฏve Bayes
Formula:
๐งฎ Compute for Pass
๐งฎ Compute for Fail
๐ Final Decision
- Pass → 0.0556
- Fail → 0.025
๐ Prediction: PASS
Comments
Post a Comment