07 Naive - Bayes
07 Naive - Bayes
1. This algorithm is called “Naive” because it makes a naive assumption that each feature is independent
of other features which is not true in real life.
2. As for the “Bayes” part, it refers to the statistician and philosopher, Thomas Bayes and the theorem
named after him, Bayes’ theorem, which is the base for Naive Bayes Algorithm.
Bayes Theorm
Where,
P(A|B) is the probability of hypothesis A given the data B. This is called the posterior probability.
P(B|A) is the probability of data B given that the hypothesis A was true.
P(A) is the probability of hypothesis A being true (regardless of the data). This is called the prior
probability of A.
P(B) is the probability of the data (regardless of the hypothesis).
P(A|B) or P(B|A) are conditional probabilities P(B|A) = P(A and B)/P(A)
a. Multinomial Naive Bayes: This is mostly used for document classification problem, i.e whether a
document belongs to the category of sports, politics, technology etc. The features/predictors used by the
classifier are the frequency of the words present in the document.
b. Bernoulli Naive Bayes: This is similar to the multinomial naive bayes but the predictors are boolean
variables. The parameters that we use to predict the class variable take up only values yes or no, for
example if a word occurs in the text or not.
c. Gaussian Naive Bayes : When the predictors take up a continuous value and are not discrete, we
assume that these values are sampled from a gaussian distribution.
Below I have a training data set of weather and corresponding target variable ‘Play’ (suggesting possibilities of
playing). Now, we need to classify whether players will play or not based on weather condition. Let’s follow the
below steps to perform it.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('Social_Network_Ads.csv')
X = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, -1].values
Splitting the dataset into the Training set and Test set
In [0]:
Feature Scaling
In [0]:
Out[5]:
GaussianNB(priors=None, var_smoothing=1e-09)
y_pred = classifier.predict(X_test)
[[65 3]
[ 7 25]]
'c' argument looks like a single numeric RGB or RGBA sequence, which should
be avoided as value-mapping will have precedence in case its length matches
with 'x' & 'y'. Please use a 2-D array with a single row if you really want
to specify the same RGB or RGBA value for all points.
'c' argument looks like a single numeric RGB or RGBA sequence, which should
be avoided as value-mapping will have precedence in case its length matches
with 'x' & 'y'. Please use a 2-D array with a single row if you really want
to specify the same RGB or RGBA value for all points.
'c' argument looks like a single numeric RGB or RGBA sequence, which should
be avoided as value-mapping will have precedence in case its length matches
with 'x' & 'y'. Please use a 2-D array with a single row if you really want
to specify the same RGB or RGBA value for all points.
'c' argument looks like a single numeric RGB or RGBA sequence, which should
be avoided as value-mapping will have precedence in case its length matches
with 'x' & 'y'. Please use a 2-D array with a single row if you really want
to specify the same RGB or RGBA value for all points.