Assignment No 2
Assignment No 2
- 2
Aim :
Implement a classification algorithm that is Naïve Bayes. Implement the following
operations:
Pre-Requisite:
Fundamentals of R -Programming Languages
Naïve Bayes classification algorithm
Naive Bayes classifiers are a collection of classification algorithms based on Bayes’ Theorem. It
is not a single algorithm but a family of algorithms where all of them share a common principle,
i.e. every pair of features being classified is independent of each other.
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 plaing
golf.
Assumption:
The fundamental Naive Bayes assumption is that each feature makes an:
Independent
Equal
We assume that no pair of features are dependent. For example, ‘Rainy’ has no effect on
the winds. Hence, the features are assumed to be independent.
Secondly, each feature is given the same weight(or importance). For example, knowing
only temperature and humidity alone can’t predict the outcome accurately. None of the
attributes is irrelevant and assumed to be contributing equally to the outcome.
Bayes’ Theorem finds the probability of an event occurring given the probability of another event
that has already occurred. Bayes’ theorem is stated mathematically as the following equation:
Basically, we are trying to find probability of event A, given the event B is true. Event B
is also termed as evidence.
P(A) is the priori of A (the prior probability, i.e. Probability of event before evidence is
seen). The evidence is an attribute value of an unknown instance(here, it is event B).
P(A|B) is a posteriori probability of B, i.e. probability of event after evidence is seen.
Now, with regards to our dataset, we can apply Bayes’ theorem in following way:
where, y is class variable and X is a dependent feature vector (of size n) where:
Naive assumption
Now, its time to put a naive assumption to the Bayes’ theorem, which is, independence among
the features. So now, we split evidence into the independent parts.
Now, as the denominator remains constant for a given input, we can remove that term:
Since, P(today) is common in both probabilities, we can ignore P(today) and find proportional
probabilities as:
and
and
and
These numbers can be converted into a probability by making the sum equal to 1 (normalization):
and
and
The method that we discussed above is applicable for discrete data. In case of continuous data, we need to
make some assumptions regarding the distribution of values of each feature
Applications:
Real time Prediction: Naive Bayes is an eager learning classifier and it is sure fast. Thus,
it could be used for making predictions in real time.
Multi class Prediction: This algorithm is also well known for multi class prediction
feature. Here we can predict the probability of multiple classes of target variable.
Text classification/ Spam Filtering/ Sentiment Analysis: Naive Bayes classifiers mostly
used in text classification (due to better result in multi class problems and independence
rule) have higher success rate as compared to other algorithms. As a result, it is widely
Input:
Structured Dataset : PimaIndiansDiabetes Dataset
File: PimaIndiansDiabetes.csv
Output:
1. Splitted dataset according to Split ratio.
2. Conditional probability of each feature.
3. Visualization of the performance of an algorithm with confusion matrix
Conclusion:
Hence, using naïve bayes classification algorithm, the classification on Pima Indians
Dataset is performed using Python program