SlideShare a Scribd company logo
Naive Bayes Classifier
Bayesian Methods
 Our focus this lecture
 Learning and classification methods based on
probability theory.
 Bayes theorem plays a critical role in probabilistic
learning and classification.
 Uses prior probability of each category given no
information about an item.
 Categorization produces a posterior probability
distribution over the possible categories given a
description of an item.
Basic Probability Formulas
 Product rule
 Sum rule
 Bayes theorem
 Theorem of total probability, if event Ai is
mutually exclusive and probability sum to 1
)
(
)
|
(
)
(
)
|
(
)
( A
P
A
B
P
B
P
B
A
P
B
A
P 


)
(
)
(
)
(
)
( B
A
P
B
P
A
P
B
A
P 







n
i
i
i A
P
A
B
P
B
P
1
)
(
)
|
(
)
(
)
(
)
(
)
|
(
)
|
(
D
P
h
P
h
D
P
D
h
P 
Bayes Theorem
 Given a hypothesis h and data D which bears on the
hypothesis:
 P(h): independent probability of h: prior probability
 P(D): independent probability of D
 P(D|h): conditional probability of D given h:
likelihood
 P(h|D): conditional probability of h given D: posterior
probability
)
(
)
(
)
|
(
)
|
(
D
P
h
P
h
D
P
D
h
P 
Does patient have cancer or not?
 A patient takes a lab test and the result comes back positive.
It is known that the test returns a correct positive result in
only 99% of the cases and a correct negative result in only
95% of the cases. Furthermore, only 0.03 of the entire
population has this disease.
1. What is the probability that this patient has cancer?
2. What is the probability that he does not have cancer?
3. What is the diagnosis?
Maximum A Posterior
 Based on Bayes Theorem, we can compute the
Maximum A Posterior (MAP) hypothesis for the data
 We are interested in the best hypothesis for some
space H given observed training data D.
)
|
(
argmax D
h
P
h
H
h
MAP


)
(
)
(
)
|
(
argmax
D
P
h
P
h
D
P
H
h

)
(
)
|
(
argmax h
P
h
D
P
H
h

H: set of all hypothesis.
Note that we can drop P(D) as the probability of the data is constant
(and independent of the hypothesis).
Maximum Likelihood
 Now assume that all hypotheses are equally
probable a priori, i.e., P(hi ) = P(hj ) for all hi,
hj belong to H.
 This is called assuming a uniform prior. It
simplifies computing the posterior:
 This hypothesis is called the maximum
likelihood hypothesis.
)
|
(
max
arg h
D
P
h
H
h
ML


Desirable Properties of Bayes Classifier
 Incrementality: with each training example,
the prior and the likelihood can be updated
dynamically: flexible and robust to errors.
 Combines prior knowledge and observed
data: prior probability of a hypothesis
multiplied with probability of the hypothesis
given the training data
 Probabilistic hypothesis: outputs not only a
classification, but a probability distribution
over all classes
Bayes Classifiers
Assumption: training set consists of instances of different classes
described cj as conjunctions of attributes values
Task: Classify a new instance d based on a tuple of attribute values
into one of the classes cj  C
Key idea: assign the most probable class using Bayes
Theorem.
MAP
c
)
,
,
,
|
(
argmax 2
1 n
j
C
c
MAP x
x
x
c
P
c
j



)
,
,
,
(
)
(
)
|
,
,
,
(
argmax
2
1
2
1
n
j
j
n
C
c x
x
x
P
c
P
c
x
x
x
P
j 



)
(
)
|
,
,
,
(
argmax 2
1 j
j
n
C
c
c
P
c
x
x
x
P
j



Parameters estimation
 P(cj)
 Can be estimated from the frequency of classes in the
training examples.
 P(x1,x2,…,xn|cj)
 O(|X|n•|C|) parameters
 Could only be estimated if a very, very large number of
training examples was available.
 Independence Assumption: attribute values are
conditionally independent given the target value: naïve
Bayes.


i
j
i
j
n c
x
P
c
x
x
x
P )
|
(
)
|
,
,
,
( 2
1 



i
j
i
j
C
c
NB c
x
P
c
P
c
j
)
|
(
)
(
max
arg
Properties
 Estimating instead of greatly
reduces the number of parameters (and the data
sparseness).
 The learning step in Naïve Bayes consists of
estimating and based on the
frequencies in the training data
 An unseen instance is classified by computing the
class that maximizes the posterior
 When conditioned independence is satisfied, Naïve
Bayes corresponds to MAP classification.
)
|
( j
i c
x
P )
|
,
,
,
( 2
1 j
n c
x
x
x
P 
)
( j
c
P
)
|
( j
i c
x
P
Example. ‘Play Tennis’ data
Day Outlook Temperature Humidity Wind Play
Tennis
Day1 Sunny Hot High Weak No
Day2 Sunny Hot High Strong No
Day3 Overcast Hot High Weak Yes
Day4 Rain Mild High Weak Yes
Day5 Rain Cool Normal Weak Yes
Day6 Rain Cool Normal Strong No
Day7 Overcast Cool Normal Strong Yes
Day8 Sunny Mild High Weak No
Day9 Sunny Cool Normal Weak Yes
Day10 Rain Mild Normal Weak Yes
Day11 Sunny Mild Normal Strong Yes
Day12 Overcast Mild High Strong Yes
Day13 Overcast Hot Normal Weak Yes
Day14 Rain Mild High Strong No
Question: For the day <sunny, cool, high, strong>, what’s
the play prediction?
Naive Bayes solution
Classify any new datum instance x=(a1,…aT) as:
 To do this based on training examples, we need to estimate the
parameters from the training examples:
 For each target value (hypothesis) h
 For each attribute value at of each datum instance
)
(
estimate
:
)
(
ˆ h
P
h
P 
)
|
(
estimate
:
)
|
(
ˆ h
a
P
h
a
P t
t 



t
t
h
h
Bayes
Naive h
a
P
h
P
h
P
h
P
h )
|
(
)
(
max
arg
)
|
(
)
(
max
arg x
Based on the examples in the table, classify the following datum x:
x=(Outl=Sunny, Temp=Cool, Hum=High, Wind=strong)
 That means: Play tennis or not?
 Working:
)
|
(
)
|
(
)
|
(
)
|
(
)
(
max
arg
)
|
(
)
(
max
arg
)
|
(
)
(
max
arg
]
,
[
]
,
[
]
,
[
h
strong
Wind
P
h
high
Humidity
P
h
cool
Temp
P
h
sunny
Outlook
P
h
P
h
a
P
h
P
h
P
h
P
h
no
yes
h
t
t
no
yes
h
no
yes
h
NB











x
no
x
PlayTennis
answer
no
strong
P
no
high
P
no
cool
P
no
sunny
P
no
P
yes
strong
P
yes
high
P
yes
cool
P
yes
sunny
P
yes
P
etc
no
PlayTennis
strong
Wind
P
yes
PlayTennis
strong
Wind
P
no
PlayTennis
P
yes
PlayTennis
P


















)
(
:
)
|
(
)
|
(
)
|
(
)
|
(
)
(
0053
.
0
)
|
(
)
|
(
)
|
(
)
|
(
)
(
.
60
.
0
5
/
3
)
|
(
33
.
0
9
/
3
)
|
(
36
.
0
14
/
5
)
(
64
.
0
14
/
9
)
(
0.0206
Underflow Prevention
 Multiplying lots of probabilities, which are
between 0 and 1 by definition, can result in
floating-point underflow.
 Since log(xy) = log(x) + log(y), it is better to
perform all computations by summing logs of
probabilities rather than multiplying
probabilities.
 Class with highest final un-normalized log
probability score is still the most probable.





positions
i
j
i
j
C
c
NB c
x
P
c
P
c )
|
(
log
)
(
log
argmax
j

More Related Content

Similar to bayesNaive algorithm in machine learning (20)

PDF
lecture 5 about lecture 5 about lecture lecture
anxiousanoja
 
PPT
9-Decision Tree Induction-23-01-2025.ppt
DarrinBright1
 
PPTX
Bagging_and_Boosting.pptx
ABINASHPADHY6
 
PPTX
Module 4_F.pptx
SupriyaN21
 
PDF
06 Machine Learning - Naive Bayes
Andres Mendez-Vazquez
 
PPTX
Naive Bayes.pptx
Uttara University
 
PDF
Bayesian Learning- part of machine learning
kensaleste
 
PPTX
Bayesian Learning by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
PPTX
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
PDF
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
PDF
We can apply this process to neural networks and come up with the probability...
TaoYin5
 
PPTX
Naive_hehe.pptx
MahimMajee
 
PDF
Naive bayes
Learnbay Datascience
 
PPT
ppt
butest
 
PDF
Bayes Theorem.pdf
Nirmalavenkatachalam
 
PDF
Bayesian regression algorithm for machine learning
SivaSankar306103
 
PDF
Lecture10 - Naïve Bayes
Albert Orriols-Puig
 
PDF
An introduction to Bayesian Statistics using Python
freshdatabos
 
PDF
[系列活動] Machine Learning 機器學習課程
台灣資料科學年會
 
PPTX
Supervised models
Hasan Badran
 
lecture 5 about lecture 5 about lecture lecture
anxiousanoja
 
9-Decision Tree Induction-23-01-2025.ppt
DarrinBright1
 
Bagging_and_Boosting.pptx
ABINASHPADHY6
 
Module 4_F.pptx
SupriyaN21
 
06 Machine Learning - Naive Bayes
Andres Mendez-Vazquez
 
Naive Bayes.pptx
Uttara University
 
Bayesian Learning- part of machine learning
kensaleste
 
Bayesian Learning by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
We can apply this process to neural networks and come up with the probability...
TaoYin5
 
Naive_hehe.pptx
MahimMajee
 
ppt
butest
 
Bayes Theorem.pdf
Nirmalavenkatachalam
 
Bayesian regression algorithm for machine learning
SivaSankar306103
 
Lecture10 - Naïve Bayes
Albert Orriols-Puig
 
An introduction to Bayesian Statistics using Python
freshdatabos
 
[系列活動] Machine Learning 機器學習課程
台灣資料科學年會
 
Supervised models
Hasan Badran
 

Recently uploaded (20)

PPTX
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
PPTX
recruitment Presentation.pptxhdhshhshshhehh
devraj40467
 
PDF
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
PDF
Copia de Strategic Roadmap Infographics by Slidesgo.pptx (1).pdf
ssuserd4c6911
 
PDF
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
PPTX
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
PPTX
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 
PPTX
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
PPTX
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
PDF
What does good look like - CRAP Brighton 8 July 2025
Jan Kierzyk
 
PDF
List of all the AI prompt cheat codes.pdf
Avijit Kumar Roy
 
PDF
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
PPTX
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
PDF
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
PDF
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
PPTX
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
PDF
Building Production-Ready AI Agents with LangGraph.pdf
Tamanna
 
PDF
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
PPT
Data base management system Transactions.ppt
gandhamcharan2006
 
PPTX
Rocket-Launched-PowerPoint-Template.pptx
Arden31
 
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
recruitment Presentation.pptxhdhshhshshhehh
devraj40467
 
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
Copia de Strategic Roadmap Infographics by Slidesgo.pptx (1).pdf
ssuserd4c6911
 
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
What does good look like - CRAP Brighton 8 July 2025
Jan Kierzyk
 
List of all the AI prompt cheat codes.pdf
Avijit Kumar Roy
 
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
Building Production-Ready AI Agents with LangGraph.pdf
Tamanna
 
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
Data base management system Transactions.ppt
gandhamcharan2006
 
Rocket-Launched-PowerPoint-Template.pptx
Arden31
 
Ad

bayesNaive algorithm in machine learning

  • 2. Bayesian Methods  Our focus this lecture  Learning and classification methods based on probability theory.  Bayes theorem plays a critical role in probabilistic learning and classification.  Uses prior probability of each category given no information about an item.  Categorization produces a posterior probability distribution over the possible categories given a description of an item.
  • 3. Basic Probability Formulas  Product rule  Sum rule  Bayes theorem  Theorem of total probability, if event Ai is mutually exclusive and probability sum to 1 ) ( ) | ( ) ( ) | ( ) ( A P A B P B P B A P B A P    ) ( ) ( ) ( ) ( B A P B P A P B A P         n i i i A P A B P B P 1 ) ( ) | ( ) ( ) ( ) ( ) | ( ) | ( D P h P h D P D h P 
  • 4. Bayes Theorem  Given a hypothesis h and data D which bears on the hypothesis:  P(h): independent probability of h: prior probability  P(D): independent probability of D  P(D|h): conditional probability of D given h: likelihood  P(h|D): conditional probability of h given D: posterior probability ) ( ) ( ) | ( ) | ( D P h P h D P D h P 
  • 5. Does patient have cancer or not?  A patient takes a lab test and the result comes back positive. It is known that the test returns a correct positive result in only 99% of the cases and a correct negative result in only 95% of the cases. Furthermore, only 0.03 of the entire population has this disease. 1. What is the probability that this patient has cancer? 2. What is the probability that he does not have cancer? 3. What is the diagnosis?
  • 6. Maximum A Posterior  Based on Bayes Theorem, we can compute the Maximum A Posterior (MAP) hypothesis for the data  We are interested in the best hypothesis for some space H given observed training data D. ) | ( argmax D h P h H h MAP   ) ( ) ( ) | ( argmax D P h P h D P H h  ) ( ) | ( argmax h P h D P H h  H: set of all hypothesis. Note that we can drop P(D) as the probability of the data is constant (and independent of the hypothesis).
  • 7. Maximum Likelihood  Now assume that all hypotheses are equally probable a priori, i.e., P(hi ) = P(hj ) for all hi, hj belong to H.  This is called assuming a uniform prior. It simplifies computing the posterior:  This hypothesis is called the maximum likelihood hypothesis. ) | ( max arg h D P h H h ML  
  • 8. Desirable Properties of Bayes Classifier  Incrementality: with each training example, the prior and the likelihood can be updated dynamically: flexible and robust to errors.  Combines prior knowledge and observed data: prior probability of a hypothesis multiplied with probability of the hypothesis given the training data  Probabilistic hypothesis: outputs not only a classification, but a probability distribution over all classes
  • 9. Bayes Classifiers Assumption: training set consists of instances of different classes described cj as conjunctions of attributes values Task: Classify a new instance d based on a tuple of attribute values into one of the classes cj  C Key idea: assign the most probable class using Bayes Theorem. MAP c ) , , , | ( argmax 2 1 n j C c MAP x x x c P c j    ) , , , ( ) ( ) | , , , ( argmax 2 1 2 1 n j j n C c x x x P c P c x x x P j     ) ( ) | , , , ( argmax 2 1 j j n C c c P c x x x P j   
  • 10. Parameters estimation  P(cj)  Can be estimated from the frequency of classes in the training examples.  P(x1,x2,…,xn|cj)  O(|X|n•|C|) parameters  Could only be estimated if a very, very large number of training examples was available.  Independence Assumption: attribute values are conditionally independent given the target value: naïve Bayes.   i j i j n c x P c x x x P ) | ( ) | , , , ( 2 1     i j i j C c NB c x P c P c j ) | ( ) ( max arg
  • 11. Properties  Estimating instead of greatly reduces the number of parameters (and the data sparseness).  The learning step in Naïve Bayes consists of estimating and based on the frequencies in the training data  An unseen instance is classified by computing the class that maximizes the posterior  When conditioned independence is satisfied, Naïve Bayes corresponds to MAP classification. ) | ( j i c x P ) | , , , ( 2 1 j n c x x x P  ) ( j c P ) | ( j i c x P
  • 12. Example. ‘Play Tennis’ data Day Outlook Temperature Humidity Wind Play Tennis Day1 Sunny Hot High Weak No Day2 Sunny Hot High Strong No Day3 Overcast Hot High Weak Yes Day4 Rain Mild High Weak Yes Day5 Rain Cool Normal Weak Yes Day6 Rain Cool Normal Strong No Day7 Overcast Cool Normal Strong Yes Day8 Sunny Mild High Weak No Day9 Sunny Cool Normal Weak Yes Day10 Rain Mild Normal Weak Yes Day11 Sunny Mild Normal Strong Yes Day12 Overcast Mild High Strong Yes Day13 Overcast Hot Normal Weak Yes Day14 Rain Mild High Strong No Question: For the day <sunny, cool, high, strong>, what’s the play prediction?
  • 13. Naive Bayes solution Classify any new datum instance x=(a1,…aT) as:  To do this based on training examples, we need to estimate the parameters from the training examples:  For each target value (hypothesis) h  For each attribute value at of each datum instance ) ( estimate : ) ( ˆ h P h P  ) | ( estimate : ) | ( ˆ h a P h a P t t     t t h h Bayes Naive h a P h P h P h P h ) | ( ) ( max arg ) | ( ) ( max arg x
  • 14. Based on the examples in the table, classify the following datum x: x=(Outl=Sunny, Temp=Cool, Hum=High, Wind=strong)  That means: Play tennis or not?  Working: ) | ( ) | ( ) | ( ) | ( ) ( max arg ) | ( ) ( max arg ) | ( ) ( max arg ] , [ ] , [ ] , [ h strong Wind P h high Humidity P h cool Temp P h sunny Outlook P h P h a P h P h P h P h no yes h t t no yes h no yes h NB            x no x PlayTennis answer no strong P no high P no cool P no sunny P no P yes strong P yes high P yes cool P yes sunny P yes P etc no PlayTennis strong Wind P yes PlayTennis strong Wind P no PlayTennis P yes PlayTennis P                   ) ( : ) | ( ) | ( ) | ( ) | ( ) ( 0053 . 0 ) | ( ) | ( ) | ( ) | ( ) ( . 60 . 0 5 / 3 ) | ( 33 . 0 9 / 3 ) | ( 36 . 0 14 / 5 ) ( 64 . 0 14 / 9 ) ( 0.0206
  • 15. Underflow Prevention  Multiplying lots of probabilities, which are between 0 and 1 by definition, can result in floating-point underflow.  Since log(xy) = log(x) + log(y), it is better to perform all computations by summing logs of probabilities rather than multiplying probabilities.  Class with highest final un-normalized log probability score is still the most probable.      positions i j i j C c NB c x P c P c ) | ( log ) ( log argmax j