Another post starts with you beautiful people!
Continuing our Machine Learning track today we will apply the Naive Bayes Classifier but before that we need to understand the Bayes Theorem. So let’s first understand the Bayes Theorem.
Bayes Theorem works on conditional probability. Conditional probability is the probability that something will happen, given that something else has already occurred. Using the conditional probability, we can calculate the probability of an event using its prior knowledge.
where
P(H) is the probability of hypothesis H being true. This is known as the prior probability.
P(E) is the probability of the evidence(regardless of the hypothesis).
P(E|H) is the probability of the evidence given that hypothesis is true.
P(H|E) is the probability of the hypothesis given that the evidence is there.
We can understand the above concept with a classic example of coin that I summarized as below picture-
Now understand the Naive Bayes Classifier in the following easiest way-
So you must be thinking in real world where we can apply this algo to solve a problem?
The answer is Email Classification ! To filter the Spam vs Ham.
Sound interesting right! let's start hands on to solve this email classification problem and build our model. Our goal is to train a Naive Bayes model to classify future SMS messages as either spam or ham.
We will follow below steps to achieve our goal-
- Convert the words ham and spam to a binary indicator variable(0/1)
- Convert the txt to a sparse matrix of TFIDF vectors
- Fit a Naive Bayes Classifier
- Measure your success using roc_auc_score
Importing required libraries-
I request you to please go through official document [sklearn.naive_bayes] of each library and read once.
Load our spam dataset-
Convert the spam and ham to 1 and 0 values respectively for probability testing:-
Do some cleaning:-
Split the data into test and train:-
Check for null values in spam:-
Let's predict our model:-
Looks great! with this model the success rate is 98.61%.
I hope with this real world example you can understand how easy is to apply Naive Bayes Classifier.
Meanwhile Friends! Go chase your dreams, have an awesome day, make every second count and see you later in my next post.
Comments
Post a Comment