AS Lecture 12 (Naive Bayes Classifier)
AS Lecture 12 (Naive Bayes Classifier)
Lecture # 12
2
Naïve Bayes Algorithm
• Naive Bayes is a family of probabilistic classification algorithms that are
based on Bayes' theorem. It is particularly used for classification and is
known for its simplicity, efficiency, and effectiveness.
• The "naive" part of the name comes from the assumption that the
features (attributes) used for classification are conditionally
independent, given the class label. In other words, it assumes that each
feature provides independent and equal contributions to the
probability of a given class.
• The basic concept behind Naive Bayes is to calculate the probability of a
data point belonging to a specific class based on the probabilities of the
individual features.
• It's widely used in applications like spam email classification, document
categorization, etc. 3
Basics of Probability
1
𝑃(ℎ𝑒𝑎𝑑) =
2
4
Basics of Probability
5
Basics of Probability
7
Conditional Probability
𝑷 𝑩Τ𝑨 ∙ 𝑷 𝑨
𝑃 𝑨/𝑩 =
𝑷(𝑩)
Thomas Bayes
𝑃(𝑑𝑖𝑎𝑚𝑜𝑛𝑑Τ𝑞𝑢𝑒𝑒𝑛) ∙ 𝑃(𝑞𝑢𝑒𝑒𝑛)
𝑃(𝑞𝑢𝑒𝑒𝑛/𝑑𝑖𝑎𝑚𝑜𝑛𝑑) =
𝑃(𝑑𝑖𝑎𝑚𝑜𝑛𝑑)
8
Conditional Probability
𝑃(𝑑𝑖𝑎𝑚𝑜𝑛𝑑Τ𝑞𝑢𝑒𝑒𝑛) ∙ 𝑃(𝑞𝑢𝑒𝑒𝑛)
𝑃(𝑞𝑢𝑒𝑒𝑛/𝑑𝑖𝑎𝑚𝑜𝑛𝑑) =
𝑃(𝑑𝑖𝑎𝑚𝑜𝑛𝑑)
1
𝑃(𝑑𝑖𝑎𝑚𝑜𝑛𝑑/𝑞𝑢𝑒𝑒𝑛) =
4
4
𝑃 𝑞𝑢𝑒𝑒𝑛 =
52
13
𝑃 𝑑𝑖𝑎𝑚𝑜𝑛𝑑 =
52
1 4
∙ 1
𝑃 𝑞𝑢𝑒𝑒𝑛Τ𝑑𝑖𝑎𝑚𝑜𝑛𝑑 = 4 52 =
13 13
52 9
Applied Example-I
No. Color Type Origin Stolen
1 Red Sports Domestic Yes
2 Red Sports Domestic No
3 Red Sports Domestic Yes
4 Yellow Sports Domestic No
5 Yellow Sports Imported Yes
6 Yellow SUV Imported No
7 Yellow SUV Imported Yes
8 Yellow SUV Domestic No
9 Red SUV Imported No
10 Red Sports Imported Yes
𝑃(𝑥/𝑌𝑒𝑠) = ?
𝑃(𝑥/𝑁𝑜) = ?
11
Applied Example-I
𝑃(𝑥/𝑌𝑒𝑠) = ?
3 5
𝑃(𝑌𝑒𝑠Τ𝑅𝑒𝑑)∙𝑃(𝑅𝑒𝑑) (5)∙(10) 3
𝑃 𝑅𝑒𝑑 Τ𝑌𝑒𝑠 = = 5 =
𝑃 𝑌𝑒𝑠 (10) 5
1 4
𝑃(𝑌𝑒𝑠Τ𝑆𝑈𝑉)∙𝑃(𝑆𝑈𝑉) ( )∙( ) 1
4 10
𝑃 𝑆𝑈𝑉 Τ𝑌𝑒𝑠 = = 5 =
𝑃 𝑌𝑒𝑠 (10) 5
2 5
𝑃(𝑌𝑒𝑠Τ𝐷𝑜𝑚𝑒𝑠𝑡𝑖𝑐)∙𝑃(𝐷𝑜𝑚𝑒𝑠𝑡𝑖𝑐) ( )∙( ) 2
5 10
𝑃 𝐷𝑜𝑚𝑒𝑠𝑡𝑖𝑐 Τ𝑌𝑒𝑠 = = 5 =
𝑃 𝑌𝑒𝑠 ( ) 5
10
12
Applied Example-I
𝑃(𝑥/𝑁𝑜) = ?
3
𝑃 𝑅𝑒𝑑 Τ𝑌𝑒𝑠 =
5
1
𝑃 𝑆𝑈𝑉 Τ𝑌𝑒𝑠 =
5
2
𝑃 𝐷𝑜𝑚𝑒𝑠𝑡𝑖𝑐 Τ𝑌𝑒𝑠 =
5
3 2
𝑃 𝑅𝑒𝑑 Τ𝑁𝑜 =1− =
5 5
1 4
𝑃 𝑆𝑈𝑉 Τ𝑁𝑜 =1− =
5 5
2 3
𝑃 𝐷𝑜𝑚𝑒𝑠𝑡𝑖𝑐 Τ𝑁𝑜 = 1 − =
5 5 13
Applied Example-I
𝑃(𝑥/𝑌𝑒𝑠) = ?
3
𝑃 𝑅𝑒𝑑 Τ𝑌𝑒𝑠 =
5
1
𝑃 𝑆𝑈𝑉 Τ𝑌𝑒𝑠 =
5
2
𝑃 𝐷𝑜𝑚𝑒𝑠𝑡𝑖𝑐 Τ𝑌𝑒𝑠 =
5
16
Applied Example-II
17
Applied Example-II
𝑃 𝑥 Τ𝑀𝑎𝑛𝑔𝑜 = 𝑃(𝑀𝑎𝑛𝑔𝑜) ∙ 𝑃 𝑌𝑒𝑙𝑙𝑜𝑤 Τ𝑀𝑎𝑛𝑔𝑜 ∙ 𝑃 𝑆𝑤𝑒𝑒𝑡Τ𝑀𝑎𝑛𝑔𝑜 ∙ 𝑃 𝐿𝑜𝑛𝑔Τ𝑀𝑎𝑛𝑔𝑜
800
𝑃 𝑌𝑒𝑙𝑙𝑜𝑤 = = 0.66
1200
850
𝑃 𝑆𝑤𝑒𝑒𝑡 = = 0.70
1200
400
𝑃 𝐿𝑜𝑛𝑔 = = 0.33
1200
19
Applied Example-II
350
𝑃(𝑀𝑎𝑛𝑔𝑜Τ𝑌𝑒𝑙𝑙𝑜𝑤)∙𝑃(𝑌𝑒𝑙𝑙𝑜𝑤) (800)∙(0.66)
𝑃 𝑌𝑒𝑙𝑙𝑜𝑤 Τ𝑀𝑎𝑛𝑔𝑜 = = = 0.533
𝑃 𝑀𝑎𝑛𝑔𝑜 (0.541)
450
𝑃(𝑀𝑎𝑛𝑔𝑜Τ𝑆𝑤𝑒𝑒𝑡)∙𝑃(𝑆𝑤𝑒𝑒𝑡) (850)∙(0.70)
𝑃 𝑆𝑤𝑒𝑒𝑡Τ𝑀𝑎𝑛𝑔𝑜 = = = 0.685
𝑃 𝑀𝑎𝑛𝑔𝑜 (0.541)
0
𝑃(𝑀𝑎𝑛𝑔𝑜Τ𝐿𝑜𝑛𝑔)∙𝑃(𝐿𝑜𝑛𝑔) ( )∙(0.33)
400
𝑃 𝐿𝑜𝑛𝑔Τ𝑀𝑎𝑛𝑔𝑜 = = =0
𝑃 𝑀𝑎𝑛𝑔𝑜 (0.541)
20
Applied Example-II
𝑃 𝑌𝑒𝑙𝑙𝑜𝑤 Τ𝑀𝑎𝑛𝑔𝑜 = 0.533
𝑃 𝑆𝑤𝑒𝑒𝑡Τ𝑀𝑎𝑛𝑔𝑜 = 0.685
𝑃 𝐿𝑜𝑛𝑔Τ𝑀𝑎𝑛𝑔𝑜 = 0
21
Applied Example-II
400
𝑃(𝐵𝑎𝑛𝑎𝑛𝑎 Τ𝑌𝑒𝑙𝑙𝑜𝑤)∙𝑃(𝑌𝑒𝑙𝑙𝑜𝑤) ( )∙(0.66)
800
𝑃 𝑌𝑒𝑙𝑙𝑜𝑤 Τ𝐵𝑎𝑛𝑎𝑛𝑎 = = =1
𝑃 𝐵𝑎𝑛𝑎𝑛𝑎 (0.33)
300
𝑃(𝐵𝑎𝑛𝑎𝑛𝑎Τ𝑆𝑤𝑒𝑒𝑡)∙𝑃(𝑆𝑤𝑒𝑒𝑡) ( )∙(0.70)
850
𝑃 𝑆𝑤𝑒𝑒𝑡Τ𝐵𝑎𝑛𝑎𝑛𝑎 = = = 0.74
𝑃 𝐵𝑎𝑛𝑎𝑛𝑎 (0.33)
350
𝑃(𝐵𝑎𝑛𝑎𝑛𝑎Τ𝐿𝑜𝑛𝑔)∙𝑃(𝐿𝑜𝑛𝑔) ( )∙(0.33)
400
𝑃 𝐿𝑜𝑛𝑔Τ𝐵𝑎𝑛𝑎𝑛𝑎 = = = 0.875
𝑃 𝐵𝑎𝑛𝑎𝑛𝑎 (0.33)
22
Applied Example-II
𝑃 𝑌𝑒𝑙𝑙𝑜𝑤 Τ𝐵𝑎𝑛𝑎𝑛𝑎 = 1
𝑃 𝑆𝑤𝑒𝑒𝑡Τ𝐵𝑎𝑛𝑎𝑛𝑎 = 0.74
𝑃 𝐿𝑜𝑛𝑔Τ𝐵𝑎𝑛𝑎𝑛𝑎 = 0.875
23
Applied Example-II
50
𝑃(𝑂𝑡ℎ𝑒𝑟𝑠 Τ𝑌𝑒𝑙𝑙𝑜𝑤)∙𝑃(𝑌𝑒𝑙𝑙𝑜𝑤) (800)∙(0.66)
𝑃 𝑌𝑒𝑙𝑙𝑜𝑤 Τ𝑂𝑡ℎ𝑒𝑟𝑠 = = = 0.33
𝑃 𝑂𝑡ℎ𝑒𝑟𝑠 (0.125)
100
𝑃(𝑂𝑡ℎ𝑒𝑟𝑠Τ𝑆𝑤𝑒𝑒𝑡)∙𝑃(𝑆𝑤𝑒𝑒𝑡) ( )∙(0.70)
850
𝑃 𝑆𝑤𝑒𝑒𝑡Τ𝑂𝑡ℎ𝑒𝑟𝑠 = = = 0.658
𝑃 𝑂𝑡ℎ𝑒𝑟𝑠 (0.125)
50
𝑃(𝑂𝑡ℎ𝑒𝑟𝑠 Τ𝐿𝑜𝑛𝑔)∙𝑃(𝐿𝑜𝑛𝑔) ( )∙(0.33)
400
𝑃 𝐿𝑜𝑛𝑔Τ𝑂𝑡ℎ𝑒𝑟𝑠 = = = 0.33
𝑃 𝑂𝑡ℎ𝑒𝑟𝑠 (0.125)
24
Applied Example-II
𝑃 𝑌𝑒𝑙𝑙𝑜𝑤 Τ𝑂𝑡ℎ𝑒𝑟𝑠 = 0.33
𝑃 𝑆𝑤𝑒𝑒𝑡Τ𝑂𝑡ℎ𝑒𝑟𝑠 = 0.658
𝑃 𝐿𝑜𝑛𝑔Τ𝑂𝑡ℎ𝑒𝑟𝑠 = 0.33
25
Applied Example-II
26
Applied Example-III
Day Outlook Temperature Humidity Wind Play Golf
1 Sunny Hot High Weak No
2 Sunny Hot High Strong No
3 Overcast Hot High Weak Yes
4 Rain Mild High Weak Yes
5 Rain Cool Normal Weak Yes
6 Rain Cool Normal Strong No
7 Overcast Cool Normal Strong Yes
8 Sunny Mild High Weak No
9 Sunny Cool Normal Weak Yes
10 Rain Mild Normal Weak Yes
11 Sunny Mild Normal Strong Yes
12 Overcast Mild High Strong Yes
13 Overcast Hot Normal Weak Yes
14 Rain Mild High Strong No
27
Applied Example-III
Outlook = Sunny
Temperature = Cool
Humidity = High
Wind = Strong
28
Applied Example-III
9
𝑃 𝑃𝑙𝑎𝑦 𝐺𝑜𝑙𝑓 Τ𝑌𝑒𝑠 = = 0.64
14
5
𝑃 𝑃𝑙𝑎𝑦Τ𝑁𝑜 = = 0.36
14
29
Applied Example-III
Outlook Yes No
2 3
Sunny = 0.222 = 0.6
9 5
4 0
Overcast = 0.444 =0
9 4
3 2
Rain = 0.333 = 0.4
9 5
30
Applied Example-III
Humidity Yes No
3 4
High = 0.333 = 0.8
9 5
6 1
Normal = 0.666 = 0.2
9 4
31
Applied Example-III
Humidity Yes No
3 3
Strong = 0.333 = 0.6
9 5
6 2
Weak = 0.666 = 0.4
9 5
32
Applied Example-III
Temperature Yes No
2 2
Hot = 0.222 = 0.4
9 5
4 2
Mild = 0.444 = 0.4
9 5
3 1
Cool = 0.333 = 0.2
9 5
33
Applied Example-II
𝒗𝑵𝑩 = 𝒂𝒓𝒈𝒎𝒂𝒙𝑷𝒋∈{𝒀𝒆𝒔,𝑵𝒐} 𝑷(𝒗𝒋 ) ∙ ς𝒊 𝑷(𝒂𝒊 |𝒗𝒋 )
34
Applied Example-II
35
Applied Example-II
Probability Normalization
Consider a football game between two rival teams, say team A and
team B. Suppose team A wins 65% of the time and team B wins the
remaining matches. Among the games won by team A, only 35% of
them comes from playing at team B’s football field. On the other
hand, 75% of the victories for team B are obtained while playing at
home.
1. If team B is to host the next match between the two teams, what
is the probability that it will emerge as the winner?
2. It team B is to host the next match between the two teams, who
will emerge as the winner?
38
Assignment - Solution
Let
Y = Winning football match
X = Hosting football match
39
Assignment - Solution
1. If team B is to host the next match between the two teams, what
is the probability that it will emerge as the winner?
40
Assignment - Solution
2. It team B is to host the next match between the two teams, who
will emerge as the winner?
41
Acknowledgment
• [Peter Andrew Bruce] Practical Statistics for Data Scientists
• [David Forsyth] Probability and Statistics for Computer Science
• [Michael Baron] Probability and Statistics for Computer Scientists
• .
42