Naive Bayes
Naive Bayes
Naive Bayes
Pros and Cons of Naive Bayes
Pros
▪ It’s relatively simple to understand and build
▪ It’s easily trained, even with a small dataset
▪ It’s fast!
▪ It’s not sensitive to irrelevant features
Cons
It assumes the every feature is independent, which
isn’t always the case in the reality.
Naive Bayes
Example of Observation
Consider a fictional dataset that describes the weather conditions for
playing a game of golf. Given the weather conditions, each tuple
classifies the conditions as fit(“Yes”) or unfit(“No”) for playing golf
The dataset is divided into two parts, namely, feature matrix and the
response vector.
▪ Feature matrix contains all the vectors(rows) of dataset in which
each vector consists of the value of dependent features. In above
dataset, features are ‘Outlook’, ‘Temperature’, ‘Humidity’ and
‘Windy’.
▪ Response vector contains the value of class variable(prediction or
output) for each row of feature matrix. In above dataset, the class
variable name is ‘Play golf’.
Naive Bayes
A Fictional Dataset
Naive Bayes
Interpretation of Assumptions
Naive Bayes
Note
Naive Bayes
Note
Naive Bayes
Applying the Theorem of Bayes
𝑃 𝑋|𝑦 𝑃 𝑦
𝑃 𝑦|𝑋 =
𝑃 𝑋
X = ( x 1 , x2 , x3 . … , x n)
Naive Bayes
Note
Naive Bayes
Naive Assumption
𝑃 𝑦|𝑥1 𝑥2 𝑥3 … ,x𝑛 =?
Naive Bayes
Naive Assumption
y = argmax𝑦 𝑃 𝑦 ෑ 𝑃 𝑥𝑖 |𝑦
i=1
The only calculation for this formula is to compute the probability P(y)
and P(xi | y). P(y) is also called class probability and P(xi | y) is called
conditional probability.
Naive Bayes
Weather Dataset
Naive Bayes
Outlook
Naive Bayes
Temperature
Naive Bayes
Humidity
Naive Bayes
Windy
Naive Bayes
Class Probability
Yes 9 9/14
No 5 5/14
Total 14 100%
Naive Bayes
Example
Naive Bayes
Example
𝑃 X=today|𝑦 𝑃 𝑦
𝑃 𝑦|X=today =
𝑃 X=today
Since the question is whether the decision to play or not to play, the
given information (prior information) is y = yes or y = no if the
condition is today (evidence). Therefore, the computation must be
done for P(y = yes | X) and P(y = no | X).
Naive Bayes
Example
𝑃 y=yes|𝑋
𝑃 𝑥1 =sunny|y=yes 𝑃 𝑥2 =hot|y=yes 𝑃 𝑥3 =normal|y=yes 𝑃 𝑥4 =false|y=yes 𝑃 y=yes
=
𝑃 X=today
𝑃 y=no ∨ 𝑋
𝑃 𝑥1 =sunny|y=no 𝑃 𝑥2 =hot|y=no 𝑃 𝑥3 =normal|y=no 𝑃 𝑥4 =false|y=no 𝑃 y=no
=
𝑃 X=today
Naive Bayes
Example
𝑃 y=yes|𝑋
∝ 𝑃 𝑥1 =sunny|y=yes 𝑃 𝑥2 =hot|y=yes 𝑃 𝑥3 =normal|y=yes 𝑃 𝑥4 =false|y=yes 𝑃 y=yes
P(y=yes|X = today) = ?
𝑃 y=no|𝑋
∝ 𝑃 𝑥1 =sunny|y=no 𝑃 𝑥2 =hot|y=no 𝑃 𝑥3 =normal|y=no 𝑃 𝑥4 =false|y=no 𝑃 y=no
P(y=no|X = today) = ?
Naive Bayes