Naive Bayes Classifier Algorithm in Machine Learning & Python.

MD RASHEDIN
4 min readAug 25, 2022

--

→ NAIVE BAYES CALSSIFIER, basically it’s a combination of bayes theorem and be naive assumption that two events are going to be independent when they not be but the Naive assumption makes the math much much easier.

Now, what is Bayes theorem(Conditional Probability) ?→

→ The conditional probability of an event A is the probability that the event will occur given knowledge that an event B has already occurred.

Now the Formula is :-

P(A|B) = P(B|A) * P(A) / P(B)

where,

P(A|B) :- Probability of A being true while B is already true.

P(B|A) :- Probability of B being true when A is already true.

P(A) :- Probability of A being true.

P(B) :- Probability of being true.

Use Case :-

Let’s find out the chances that guests are coming our home to attend a Birthday function while raining.

P(B) → There is 15% probability that then guests are coming to our home to attend function.

P(A) → Suppose there is 10% guests that if the rain stopes the guest will come to home.

P(B|A) →Probability of 5% sweet , lovable , and kind hearted guests are coming to attend the function even while raining outside, those kinds of guests are very rare.

P(A|B) → Now, we have to find the probability that guests are coming to attend the function while raining.

P(A|B) = P(B|A) * P(A) / P(B)

Now we put our data into this,

0.05 * 0.10 / 0.15 = 0.333

Probability that guests are not coming to attend the function ; 1- 0.333 = 0.667

Hence, there is 33% chance that guests are coming to attend the function and the 67% chance that guests are not coming to attend the function.

Now we will focus on Naive Bayes Classifier Types :-

  1. Bernoulli Distribution :-

→ A random experiment whose outcomes are only of two types, say success S and failure F, is a Bernoulli trial. The probability of success is taken as p while that of failure is q = 1 − p. A random experiment whose outcomes are only of two types, say success S and failure F, is a Bernoulli trial. The probability of success is taken as p while that of failure is q = 1 − p.

P(success) = p

P(failure) = q = 1-p

2. Multinomial Distribution :-

→ Suppose, there is a text document & we are counting a particular word occurring again & again, then we use Multinomial Distribution.

→ We can say we use multinomial for Discrete Count.

P(X1=x1,X2=x2,…………….Xk=xk) {where k =1,2,3… as we know}

for example :- In a city there’s some people whose blood groups are following.

taking some short example like 6 Indians :

1 : O, 2 : A, 2 : B, 1 : AB

p(x1=1, x2=2, x3 = 3, x4 =1)

so, 6! * (0.44 * 0.42 * 0.10* 0.04) / (1! * 2! * 2! * 1!)

maybe something about 0.133056 so, approx 13%

3. Gaussian Distribution (Normal Distribution) :-

  • Don’t use in discrete count
  • Use if variables are in continuous nature
  • for example ; iris dataset
  • ∞ < x < ∞

Here we will work on titanic dataset and learn the Gaussian Naive Bayes classifier ;

First we will import the libraries.

then we will drop unnecessary columns & we will embed the main columns in input variable & survived which is our target column so we will put in target variable.

and then we will either replace male or female with 1 & 0 or we will use get_dummies to make male and female a new columns and after that we will drop sex column.

then we will check for null values if these is then we will fill by the mean of that column.

then check for float values if there is then convert it into the int type.

then we will train our model.

and will check the size of train & test and then we will analyze them.

then we will import our model GaussianNB()

after that we will find accuracy score and then we will predict.

Here is the source code of my Google Colab file Link :-

Link :- https://colab.research.google.com/drive/1hcdUP8AXC_ts-Nxab0jWpMq0gMlrfegc?usp=sharing

--

--