Machine Learning Tutorial Python - 15: Naive Bayes Classifier Algorithm Part 2

codebasics · Beginner ·🔢 Mathematical Foundations ·6y ago

Key Takeaways

Builds an email spam classifier using Naive Bayes algorithm and sklearn in Python

Full Transcript

here is a list of topics we are going to cover in this video in the end we have an exercise for use of watch tilde and I have this file with spam emails where I have the text body of the email and the first column says whether it is ham or spam ham means it's a good email it is not a spam okay and you can see that whenever you have spam you have Tom's like free message or free entry or winner you know this just by reading it it definitely looks like a spam I have loaded this particular CSV file in a panda's data frame as usual and I'm going to now do some data exploration to see what's going on with this data the first thing I am doing is just grouping it by category and describing it so that I know that there are four thousand eight hundred and twenty fives hem and 744 for the seven spam okay so that much I know that a good amount of spam in our data set the spam column is text I want to convert it to a numbers because we all know that machine learning models they understand numbers they don't understand text so we have to convert the category and message both the columns into numbers somehow okay the first one is very easy category ham and spam we can use 1 and 0 so that's what we are going to do and the way to do that is by using this apply function here when I say DF category it takes the category column and applies this particularly aMDA function on it the lambda function takes each individual values and checks if it is spam then it will put a return a value of 1 otherwise 0 and we will create a new column called spam here and when you execute this you can see that all the ham for 0 the spam are 1 here once that is done we can import the train to split method from Ascalon as usual here I have imported it and I'm gonna keep my tastes size to be 25% and when you run it it's it is splitting the samples into train and taste dataset okay once this is done we still have a message column which is text so that text column we definitely want to convert into indoor numbers now the way we'll do this is using count vectorizer technique in count vector as a technique let's say you have these four documents or the email bodies with all this text one of the ways to convert this into matrix or a vector is you find out the unique words in each of these documents and you'll find that there are nine unique words combinely in all of these documents now you can take each of these documents and you can treat these nine unique words as features or kind of like a column and you can build this kind of matrix okay here this is the first document second document and so on the first document you say end is 0 the occurrence of end is zero times you see that there is zero document appeared once first appeared once similarly in the second document the document appeared too so you say document here document here so this is a simple technique of representing versus count okay and we can use these individual columns as feature for our problem I took this example from SK learn documentation and here is a code snippet from SK learn documentation it explains the same thing but with the Escalon api and these are the AP as we are going to use for our data set I used count vectorizer and created the metrics which I showed you in the picture so it created probably many features that's why you see dot dot dot and these features are equal to number of unique words in our corpus corpus is basically all the unique words that you see in this huge data so you realize it will be many columns in over metric now naivebayes have three kind of classifiers like Bernoulli multimode multinomial and Gaussian in the last tutorial we use caution I bias here is the Quran Sarang what's the difference between the three and this guy accepting really gave a good answer where burn only is basically when your features okay features not the target variable when your features are 0 or 1 they're binary in nature that's when you use this multinomial is when you have discrete data for example your movie rating ranging from 1 to 5 Gaussian naive Bayes is when you have normal distribution or a bell curve in your features ok we are going to use multinomial naive bayes here for our problem and the way you do that is by writing this code just to say oh the typing time I'm just copying and pasting the code but you kind of get it you run model dot fit function on your x train count and VY train remember the X train count is basically the text which is the emails converted into a number metric once the model is trained it is ready to make a prediction so let's have two emails okay so we have this two image now the first one looks like a good email where a friend is asking another friend to go for a football game and the other one clearly looks like a spam and when you run it you see it detected the second email as one which is it's a spam all right now let's measure the accuracy or the score and the way you do that is first X test you need to convert it into count because our model is designed such that it works only on numbers and then you can feed it to model for election and you find that the model performs really well with 98 posts probability so you can see that for spam non-spam type of problem the naive Bayes model works really great now you found that the converting it into a metric create a little bit of inconvenience in dumps of when I was supplying X test count I had to perform this transform method also when I was trying this test emails I had to call this transform method before giving it to my model Escalon has a nice feature called pipeline where you can define a pipeline of your transformation here what we are doing is on our raw data we applying some sort of transformation before feeding it into our model right now we use only count vectorizer some people use more than one one transformation people you like tf-idf and so on so in that case if you have SQL on pipeline it is super useful and convenient and I am going to show you how to use the pipeline so the first thing you do is you import the pipeline like this and then you create the pipeline using your pipeline steps so my first step is count vectorizer so just to remind you on what I am doing here is I'm trying to simplify the same code base so the code work till here ok our model is ready it score 98% fine but since we had to perform this transformation steps I am writing the same core using a simple API and here I created a pipeline with two steps first step is convert my text into the vector of count vectorizer and then apply the multinomial naive bayes and when I have my classifier created what I am doing is I am going to train it now this time when I train it I can train directly on X train so remember what is X train Xtreme has this text okay in the previous example we use X train count we converted X into count and the entry in the model here we can directly feed the text into our model because internally this pipeline will convert to a vector first and then it will apply now naivebayes on that so when you run it you can see this works okay and again you can project the performance of our classifier it is same 98% okay and just to verify if the emails prediction works okay you can run this and you can see the first email is not spam the second one is spam and those are these two emails all right now comes the most interesting part of my tutorial which is the exercise I always give this example that if you want to learn swimming by watching swimming videos you're not going to learn swimming okay what do you do you have to move your butt and jump in the swimming pool similarly if you want to know coding you have to code and I have prepared these exercises with so much effort for you so why don't you take your laptop and just work on this naive based exercise all you have to do is load Escalon datasets find it ahead and classify those wines into one of the three categories using naive bayes you can use gaussian and multinomial classifier and tell me which one performs the best in the comments below i had a link of this file exercise file in the video description below also the tutorial code that was shown in this video that code link is also available in the video description below so all this code is available so you can just go ahead and try it there is a solution link by the way but a good student will not click on the link without trying it first on his own so I assume you all are beautiful good students you will try this thing on your own and then only look at the solution to match your answer thank you very much if you liked this video give it a thumbs up share this with your friends subscribe to my channel and please post a feedback in the comments below it really helped me it helped me improve my content thank you bye

Original Description

In this python machine learning tutorial for beginners we will build email spam classifier using naive bayes algorithm. We will use sklearn CountVectorizer to convert email text into a matrix of numbers and then use sklearn MultinomialNB classifier to train our model. The model score with this approach comes out to be very high (around 98%). Sklearn pipeline allows us to handle pre processing transformations easily with its convenient api. In the end there is an exercise where you need to classify sklearn wine dataset using naive bayes. #MachineLearning #PythonMachineLearning #MachineLearningTutorial #Python #PythonTutorial #PythonTraining #MachineLearningCource #NaiveBayes #sklearntutorials #scikitlearntutorials Dataset: https://github.com/codebasics/py/blob/master/ML/14_naive_bayes Exercise: https://github.com/codebasics/py/blob/master/ML/14_naive_bayes/exercise.md Code:https://github.com/codebasics/py/blob/master/ML/14_naive_bayes/14_naive_bayes_2_email_spam_filter.ipynb Exercise solution: https://github.com/codebasics/py/blob/master/ML/14_naive_bayes/Exercise/14_naive_bayes_exercise.ipynb Topics that are covered in this Video: 00:00 explore spam email dataset 02:33 sklearn CountVectorizer 04:30 types of naive bayes classifiers 05:23 sklearn MultinomialNB classifier 06:48 sklearn pipeline 09:35 Exercise Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Next Video: Machine Learning Tutorial Python - 16: Hyper parameter Tuning (GridSearchCV): https://www.youtube.com/watch?v=HdlDYng8g9s&list=PLeo1K3hjS3uvCeTYTeyfe0-rN5r8zn9rw&index=17 Populor Playlist: Data Science Full Course: https://www.youtube.com/playlist?list=PLeo1K3hjS3us_ELKYSj_Fth2tIEkdKXvV Data Science Project: https://www.youtube.com/watch?v=rdfbcdP75KI&list=PLeo1K3hjS3uu7clOTtwsp94PcHbzqpAdg Machine learning tutorials: https://www.youtube.com/watch?v=gmvvaobm7eQ&list=PLeo1K3hjS3uvCeTYTeyfe0-rN5r8zn9rw Pandas: https://www.youtube.com/wat
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 0 of 60

← Previous Next →
1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

Related Reads

Chapters (6)

explore spam email dataset
2:33 sklearn CountVectorizer
4:30 types of naive bayes classifiers
5:23 sklearn MultinomialNB classifier
6:48 sklearn pipeline
9:35 Exercise
Up next
Marks Weightage | Quantitative Aptitude CA Foundation September 2026 | ABC Analysis | Nithin
ArivuPro Academy
Watch →