Naive Bayes Classifier - Example Problems

 

Example1: Spam Detection using Naรฏve Bayes

๐Ÿงพ Step 1: Training Dataset

Email IDContains “offer”Contains “money”Class
1YesYesSpam
2YesNoSpam
3NoYesSpam
4YesYesSpam
5NoNoNot Spam
6NoYesNot Spam
7YesNoNot Spam
8NoNoNot Spam

๐Ÿ“Š Step 2: Compute Prior Probabilities

Total emails = 8

  • Spam = 4
  • Not Spam = 4
P(Spam)=48=0.5P(Spam) = \frac{4}{8} = 0.5
P(Not Spam)=48=0.5P(Not\ Spam) = \frac{4}{8} = 0.5

๐Ÿ”— Step 3: Compute Likelihoods

For Spam

  • P(offer=YesSpam)=3/4=0.75
  • P(money=YesSpam)=3/4=0.75 

For Not Spam

  • P(offer=YesNot Spam)=1/4=0.25
  • P(money=YesNot Spam)=1/4=0.25

๐ŸŽฏ Step 4: New Email Prediction

๐Ÿ‘‰ New email contains:

  • “offer” = Yes
  • “money” = Yes

We classify this email.


๐Ÿ”ข Apply Naรฏve Bayes

Formula

P(hd)P(d1h)P(d2h)P(h)


๐Ÿงฎ Compute for Spam

P(Spamdata)0.75×0.75×0.5=0.28125

๐Ÿงฎ Compute for Not Spam

P(Not Spamdata)0.25×0.25×0.5=0.03125

๐Ÿ† 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

DayWeather    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

P(SunnyYes)=29P(Sunny|Yes) = \frac{2}{9}

For Play = No

P(SunnyNo)=35P(Sunny|No) = \frac{3}{5}

๐Ÿ“Š Step 4: Prior Probabilities

P(Yes)=914,P(No)=514P(Yes) = \frac{9}{14}, \quad P(No) = \frac{5}{14}

๐Ÿ”ข Step 5: Apply Bayes’ Theorem

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

Since P(d) is same for both classes, we compare:


๐Ÿงฎ Compute for Play = Yes

P(YesSunny)29×914=214=0.1429P(Yes|Sunny) \propto \frac{2}{9} \times \frac{9}{14} = \frac{2}{14} = 0.1429

๐Ÿงฎ Compute for Play = No

P(NoSunny)35×514=314=0.2143P(No|Sunny) \propto \frac{3}{5} \times \frac{5}{14} = \frac{3}{14} = 0.2143

๐Ÿ† 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

P(Yes)=610=0.6P(Yes) = \frac{6}{10} = 0.6
P(No)=410=0.4P(No) = \frac{4}{10} = 0.4


๐Ÿ”— Step 4: Likelihood Probabilities

For Accident = Yes (6 cases)

  • P(RainyYes)=2/6P(Rainy|Yes) = 2/6
  • P(PoorYes)=5/6P(Poor|Yes) = 5/6
  • P(HighYes)=5/6P(High|Yes) = 5/6
  • P(Engine=YesYes)=5/6P(Engine=Yes|Yes) = 5/6

For Accident = No (4 cases)

  • P(RainyNo)=1/4
  • P(PoorNo)=0/4=0P(Poor|No) = 0/4 = 0
  • P(HighNo)=0/4=0P(High|No) = 0/4 = 0
  • P(Engine=YesNo)=0/4=0P(Engine=Yes|No) = 0/4 = 0

๐Ÿ”ข Step 5: Apply Naรฏve Bayes

Formula:

P(hd)P(d1h)P(d2h)P(d3h)P(d4h)


๐Ÿงฎ Without Smoothing (for understanding)

For Yes

P(Yesdata)26×56×56×56×0.6P(Yes|data) \propto \frac{2}{6} \times \frac{5}{6} \times \frac{5}{6} \times \frac{5}{6} \times 0.6

๐Ÿ‘‰ Non-zero → valid


For No

P(Nodata)14×0×0×0×0.4=0P(No|data) \propto \frac{1}{4} \times 0 \times 0 \times 0 \times 0.4 = 0


๐Ÿ† 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

IDStudy    Attendance    Assignment    Result
1            High    Good    Yes    Pass
2High    Good    Yes    Pass
3High    Poor        Yes    Pass
4Low    Good    Yes    Pass
5Low    Poor    No    Fail
6Low    Poor    No    Fail
7High    Good    No    Pass
8Low    Good    No            Fail
9High    Poor    No    Fail
10Low    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

P(Pass)=610=0.6P(Pass) = \frac{6}{10} = 0.6
P(Fail)=410=0.4P(Fail) = \frac{4}{10} = 0.4

๐Ÿ”— Step 4: Likelihood Probabilities

For Pass (6 cases)

  • P(HighPass)=4/6P(High|Pass) = 4/6
  • P(GoodPass)=5/6P(Good|Pass) = 5/6
  • P(No AssignmentPass)=1/6P(No\ Assignment|Pass) = 1/6

For Fail (4 cases)

  • P(HighFail)=1/4P(High|Fail) = 1/4
  • P(GoodFail)=1/4P(Good|Fail) = 1/4
  • P(No AssignmentFail)=4/4=1P(No\ Assignment|Fail) = 4/4 = 1

๐Ÿ”ข Step 5: Apply Naรฏve Bayes

Formula:

P(hd)P(d1h)P(d2h)P(d3h)P(h)P(h\mid d) \propto P(d_1\mid h)\cdot P(d_2\mid h)\cdot P(d_3\mid h)\cdot P(h)


๐Ÿงฎ Compute for Pass

P(Passdata)46×56×16×0.6P(Pass|data) \propto \frac{4}{6} \times \frac{5}{6} \times \frac{1}{6} \times 0.6
=0.6667×0.8333×0.1667×0.60.0556= 0.6667 \times 0.8333 \times 0.1667 \times 0.6 \approx 0.0556

๐Ÿงฎ Compute for Fail

P(Faildata)14×14×1×0.4P(Fail|data) \propto \frac{1}{4} \times \frac{1}{4} \times 1 \times 0.4
=0.25×0.25×1×0.4=0.025= 0.25 \times 0.25 \times 1 \times 0.4 = 0.025

๐Ÿ† Final Decision

  • Pass → 0.0556
  • Fail → 0.025

๐Ÿ‘‰ Prediction: PASS

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