Documented by:
Pratyush Kumar Das | 5th Yr. Int. MSc., School of Physical Sciences, NISER | pratyushk.das@niser.ac.in |
Instructor: Dr. Subhankar Mishra | Assistant Professor | School of Computer
Sciences (CSS), NISER |
Naive Bayes is a classification technique based on Bayes theorem with an assumption of independence among predictors. It comprises of two parts Naive and Bayes. In simple terms this theorem assumes presence of a particular feature in a class is unrelated to presence of any other feature, even if these features depend upon eachother or the existence of the other features. All of these properties independently contribute to the probability. This why we call it Naive Bayes.
In probability theory and statistics, Bayes Theorem is alternatively known as the Bayes law or Bayes rule. This describes the probability of an event based on the prior knowledge of the conditions that might be realted to the event. Bayes theorem is a way to find out the conditional probability. The conditional probab is the probab of the event happening, given that it has some relationship with one or more events. For example, probability of getting a parking space is connected to the time of the day you park, where you park and few other things. This theorem is slightly more nuanced. In a nutshell it gives you an actual probability of an event given information about the tests.
Definition: Given a hypothesis H and evidence E, Bayes' theorem states that the relationship between the probability of the hypothesis before getting the evidence P(H) and the probability of the hypothesis after getting the evidence P(H|E) is:
Example: Suppose I have a deck of cards and if a single card is drawn then probability that the card will be a king is simply given by 4/52. If king is an event then P(king) = 4/52 = 1/13. Evidence is provided for instance someone looks as the card is the single card is the face card. The probability that the card is king, using Bayes theorem comes out to be,
As every king card is a face card thus P(Face|king)=1 and we have 3 type of face cards(jack, queen and king), thus P(Face)=12/52=3/13. Using bayes theroem we can find the probability of king given the card is a face card. So our final answer will be, P(king|Face)=1/3.
To understand this in other words we can say as we have only face cards and for a standard deck there are 3 types of face cards and thus choosing one of them has a probability of 1/3.
Proof for Bayes' Theorem: The conditional probability's are defined as following relations,
and
If we equate from both these equations, we will get,
To get the expression from our definition we simply have to replace A with H and B with E. Basically we managed to relate P(H) to the probability of H after getting an evidence E(=P(H|E)) and using this theorem we are checking how probable is our hypothesis given the observed evidence.
Now, lets try to implement the formulas in real life scenarios.
Similarly we can create the likelihood table for humidity and the wind.
1. Classification and categorization of news into few subsections such as National, International, Sports, Media, Travel & lifestyle, Stock market etc.
2. Spam detection of emails
3. Object and Face Reognition
4. Weather Prediction
Types of Naive Bayes1. Gaussian: Assumes the feature follow a normal distribution.
2. Multinomial: Used for discrete counts.
3. Bernoulli : Feature vectors are binary in this case.
Step-1 : Handling the Data
Step-2 : Summarizing Data
Step-3 : Making a Prediction
Step-4 : Making all the predictions
Step-5 : Evaluatte Accuracy
Step-6 : Tying all Together
Basically I have divided the entire code into these 6 parts and below you can find the code which is self-explanatory.
Alternatively one can use Sklearn and instead of this long code simply use an equally efficient and shorter version of the code. Simply import GaussianNB from sklearn.naive_bayes and use the model to fit data.