Naive Bayes Classifier

●Naive Bayes is a supervised learning algorithm, based on Bayes theorem and used to solve classification problems.

●It is a probabilistic classifier, which means it predicts on the basis of the probability of an object.

Types of Probabilities

● Prior Probability also called as Unconditional Probability

E.g. P(cavity) = 0.1, ○Probability of a patient having a cavity is 0.1 in absence of any other information

● Posterior probability also called as Conditional Probability

E.g. P(cavity/toothache) = 0.8, Probability of a patient having a cavity is 0.8 given that he has toothache

●Total probability Theorem:

●Bayes’ Theorem:

○Let X be a data sample (“evidence”): class label is unknown

○Let H be a hypothesis that X belongs to class C

○Classification is to determine P(H|X), (i.e., posteriori probability):  the probability that the hypothesis holds given the observed data sample X

Working of Naive Bayes Algorithm

1.Convert the data set into a frequency table.

2.Create Likelihood table by finding the probabilities.

3.Use Naive Bayesian equation to calculate the posterior probability

argmax chooses the value of y that gives the maximum value of the expression

E.g.

P(Spam∣X)=0.72

P(NotSpam∣X)=0.28, here X is the data sample

P(y) is the prior probability of the class.

πmeansProduct,P(xi​∣y)\pi means Product, P(x_i​∣y) mean Probability of feature xi​, given that the class is y.

e.g. P(Outlook=Sunny∣Play=Yes)

Solved Problem:

Consider Play Tennis Data set

Classify the tuple <Outlook=Sunny, Temperature=Cool, Humidity=high, Wind=strong>

For classification, calculate

1.Prior probability- “probability of yes” and “probability of no”

2.Conditional probability

Prior probability

●“Yes” is appearing 9 times

●“No” is appearing 5 times

●P(Play_tennis=yes)= 9/14

●P(Play_tennis=no)= 5/14

The weather data, with counts and probabilities
OutlookTemperatureHumidityWindPlay Tennis
YesNoYesNoYesNoYesNoYesNo
Sunny23Hot22High34False6295
Overcast40Mild42Normal61True33
Rain32Cool31
Likelihood table
Sunny2/93/5Hot2/92/5High3/94/5False6/92/59/145/14
Overcast4/90/5Mild4/92/5Normal6/91/5True3/93/5
Rain3/92/5Cool3/91/5

How the Frequency table is created?

Count the number of “Sunny” and “Yes”, there are 2 (“Sunny” and “Yes”), Similarly there are 3 (“Sunny” and “No”), So write 2 and 3 at respective place.

In the likelihood table, convert these frequencies into probabilities. So for Sunny and Yes column it will be 2/9 as total yes are 9 and sunny and yes are 2. Similarly for sunny and no it will be 3/9.

Likewise write for all the values.

Ynb(yes) = p(yes)*( P(sunny/yes) * P(cool/yes) * P(high/yes) * P(strong/yes))

=29×39×39×39×914=0.0053=\frac{2}{9}\times\frac{3}{9}\times\frac{3}{9}\times\frac{3}{9}\times\frac{9}{14}=0.0053

Ynb(no) = p(no)*( P(sunny/no) * P(cool/no) * P(high/no) * P(strong/no))

=35×15×45×35×514=0.0206=\frac{3}{5}\times\frac{1}{5}\times\frac{4}{5}\times\frac{3}{5}\times\frac{5}{14}=0.0206

Ypredict = max(Ynb(yes) , Ynb(no) )

Maximum value is that of No, therefore Naive Bayes Classifier will predict a “No”, So Play Tennis is “No” for the given tuple.