Building Email SPAM Detector with Naïve Bayes and AdaBoost Machine Learning Classifiers in JupyterLab

Natural language processing is a sub-branch of artificial intelligence. Building a machine or a tool to process the data through natural language processing requires mathematics, statistics, algorithms, and Python programming. Advanced techniques such as Word2Vec can convert words into vectors which makes it easier to process the text through mathematics and deep learning algorithms. Python language can handle the language humans speak, write, and understand. Before we begin the practical implementation of Python code in JupyterLab, it is critical to understand the essentials of natural language processing and machine learning classifiers. The following figure depicts the multiple branches of natural language processing.

It is crucial to set the context and background problem in an enterprise that we are trying to solve through natural language processing and machine learning in Python. Once the background problem statement is defined, the next step is to identify the dataset and preprocess the data to prepare the data in a way a machine can understand through Python language. Feature engineering is another critical aspect of data science problem to process the linguistics of the text. Any machine learning classifier can be applied to the preprocessed dataset to identify an email either as spam or non-spam. In this scenario, I will be processing a preprocessed dataset that contains 48 columns. It is essential to determine the word-frequency measure to understand the number of times a particular word appeared in the dataset divided by the number of words in the document multiplied by 100. In this preprocessed dataset from UCI, the last column has been identified as the label; one = SPAM and 0 = not SPAM. The utilization of this dataset does not require much data wrangling and preprocessing for measuring the accuracy of the classification through Naïve Bayes and AdaBoost classifiers.

There are several other machine learning classifiers that can be applied to resolve the email spam detection problem as part of the supervised machine learning algorithms. As part of solving the problem, initially a trained classification model will be generated that learns the data features from the training samples. Once the training is complete, it will be able to identify any new data through classification. Binary classification is the most common classifier that can determine if the email is SPAM or non-SPAM. When multiclass classification is applied on such problem, it can allow more than just two possible classes above and beyond binary classification with more outcomes. Handwritten recognition goes way back for being a research and development problem to identify the digits from 0 to 9 on several bank check systems. Multi-label classification is another type of algorithm typically applied in bioinformatics and genomics, where a protein can have multiple functions. In this scenario, breaking down the number of classifiers can be a solution for label classification from a multi-label classifier into many binary classifiers.

Figure 2: Adapted from Python Machine Learning by Example.

The Bayes’ theorem denotes event A and event B. Prediction of the weather, such as will there be a storm tomorrow or the probability of getting a head or a tail when flipped. The probability of P(A|B) is the probability of hypothesis event A for the data B. Here P(A|B) represents the posterior probability. P(B|A) represents the probability of observing B for the event A. P(A) denotes the probability of A being true. It is the prior probability of A. P(B) denotes the probability of the data event B.

Figure 3: Bayes’ Theorem

Python environment has to be set up on macOS or on Linux environment. There will not be a step-by-step guide for installing Python on macOS or Linux as part of this article. Either PyCharm or Anaconda distributions can be downloaded to set up Python and JupyterLab environments. Individual packages have to be imported if PyCharm has been chosen as the Python environment through Pip command. If Anaconda environment has been set up, it installs all the necessary Python 3.6 packages so that there won’t be any issues when Naïve Bayes classifier is accessed from sci-kit learn. Most of the time, there’s massive preprocessing for natural language processing. As long as the data does not have any imbalances and it is a fit any machine learning classifier can be applied to classify the SPAM problem in the emails. Python sci-kit learn comes with Naïve Bayes classifier for multinomial models. Multinomial Naïve Bayes classifier works with high accuracy for any discrete features such as word counts. However, it is also known to work well for TF-IDF as well. The data has been shuffled to access different chunks of data in Python program and the last 100 rows have been considered for training and testing. The machine learning model with Naïve Bayes classifier has shown an accuracy of 87%. Any other classifier can be applied as well. AdaBoost classifier has demonstrated an accuracy of 93%.

Results

The JupyterLab Notebook has been shared on Github at GPSingularity.

References

Cox, T. (2018). Raspberry Pi 3 Cookbook for Python Programmers — Third Edition (3 ed.). Birmingham, England: Packt Publishing.

Hardeniya, N. (2016). Natural Language Processing: Python and NLTK . Birmingham, England: Packt Publishing.

Liu, Y. H. (2017). Python Machine Learning By Example . Birmingham, England: Packt Publishing.

Thanaki, J. (2017). Python Natural Language Processing. Birmingham, England : Packt Publishing.

27 thoughts on “Building Email SPAM Detector with Naïve Bayes and AdaBoost Machine Learning Classifiers in JupyterLab

  1. adidas ultra boost Posted On

    I actually wanted to type a brief comment to be able to appreciate you for all of the stunning solutions you are posting on this website. My particularly long internet investigation has now been compensated with reasonable know-how to write about with my family and friends. I would say that many of us readers are very much blessed to be in a great site with very many brilliant individuals with good concepts. I feel extremely privileged to have used your entire webpages and look forward to plenty of more fun minutes reading here. Thanks again for a lot of things.

    Reply
  2. adidas yeezy boost Posted On

    I have to voice my appreciation for your kind-heartedness in support of men who require help on this particular field. Your very own commitment to passing the solution all around was amazingly invaluable and has frequently helped people just like me to attain their objectives. The helpful guide implies a lot a person like me and even more to my fellow workers. With thanks; from everyone of us.

    Reply
  3. john wall shoes Posted On

    I simply wanted to write down a brief remark so as to express gratitude to you for these stunning ways you are placing here. My time consuming internet lookup has at the end of the day been paid with pleasant details to talk about with my family and friends. I ‘d express that most of us website visitors actually are very lucky to be in a really good site with many special people with very helpful points. I feel rather happy to have come across your webpages and look forward to really more cool minutes reading here. Thanks once more for a lot of things.

    Reply
  4. golden goose sneakers Posted On

    I really wanted to write a quick word in order to thank you for the magnificent solutions you are giving here. My incredibly long internet research has now been recognized with reasonable details to exchange with my great friends. I would point out that many of us site visitors actually are undoubtedly blessed to dwell in a superb place with so many special individuals with valuable principles. I feel rather fortunate to have encountered your entire webpage and look forward to really more fun times reading here. Thanks a lot once again for everything.

    Reply
  5. yeezy boost Posted On

    I simply had to say thanks all over again. I’m not certain what I might have made to happen in the absence of the actual methods documented by you regarding such a subject matter. It was a frightful circumstance in my view, but being able to see this specialized mode you handled that made me to jump for delight. I’m just grateful for this support and then expect you realize what a great job you are undertaking training many people using a web site. Probably you’ve never encountered any of us.

    Reply
  6. chrome hearts Posted On

    My wife and i ended up being absolutely ecstatic when Peter could do his reports from your precious recommendations he made from your web pages. It’s not at all simplistic to just possibly be making a gift of secrets that many the rest could have been trying to sell. We really consider we’ve got you to thank because of that. The entire illustrations you have made, the simple web site navigation, the relationships you will aid to foster – it’s all fabulous, and it’s really leading our son and our family do think that subject is amusing, and that’s exceptionally important. Many thanks for the whole lot!

    Reply
  7. nmd Posted On

    I enjoy you because of your whole efforts on this web page. My niece delights in participating in investigations and it’s simple to grasp why. Many of us notice all about the lively mode you convey useful steps on the web site and in addition invigorate contribution from others on that subject then our own child is really being taught a lot. Enjoy the remaining portion of the year. You have been conducting a remarkable job.

    Reply
  8. yeezy shoes Posted On

    This is the correct blog for anybody who desires to find out about this topic. You notice so much its almost exhausting to argue with you (not that I truly would want匟aHa). You positively put a new spin on a topic thats been written about for years. Great stuff, simply great!

    Reply
  9. Adidas NMD Runner Black White Fixed Posted On

    I want to show my appreciation to this writer just for rescuing me from this particular crisis. As a result of searching through the online world and getting suggestions which were not pleasant, I figured my entire life was well over. Living minus the strategies to the problems you’ve fixed as a result of the report is a crucial case, and the kind that could have in a negative way affected my career if I hadn’t noticed your website. Your good know-how and kindness in controlling every part was crucial. I am not sure what I would have done if I hadn’t encountered such a point like this. I can also at this point look ahead to my future. Thanks so much for this skilled and amazing guide. I will not hesitate to endorse the blog to anyone who would need tips on this situation.

    Reply
  10. yeezy boost 350 Posted On

    Nice post. I learn one thing tougher on completely different blogs everyday. It is going to all the time be stimulating to read content from different writers and practice just a little one thing from their store. I抎 want to use some with the content on my weblog whether you don抰 mind. Natually I抣l give you a link in your net blog. Thanks for sharing.

    Reply
  11. harden shoes Posted On

    That is the precise weblog for anybody who desires to search out out about this topic. You realize a lot its almost onerous to argue with you (not that I actually would need匟aHa). You undoubtedly put a new spin on a subject thats been written about for years. Nice stuff, simply great!

    Reply
  12. nfl store Posted On

    I used to be more than happy to find this net-site.I wanted to thanks on your time for this excellent learn!! I positively enjoying every little little bit of it and I have you bookmarked to take a look at new stuff you blog post.

    Reply
  13. adidas ultra boost 3.0 Posted On

    I must express thanks to the writer for rescuing me from this type of dilemma. Just after surfing throughout the search engines and coming across ideas which were not helpful, I believed my entire life was done. Living without the solutions to the difficulties you have solved by way of your site is a serious case, and the kind that might have adversely affected my career if I had not noticed your web site. Your actual know-how and kindness in controlling a lot of stuff was important. I’m not sure what I would’ve done if I had not come across such a solution like this. I can at this point look forward to my future. Thanks for your time very much for your high quality and sensible help. I will not be reluctant to refer the website to anybody who should receive recommendations on this issue.

    Reply
  14. oakley sunglasses for men Posted On

    The following time I read a weblog, I hope that it doesnt disappoint me as much as this one. I mean, I do know it was my choice to learn, but I truly thought youd have something attention-grabbing to say. All I hear is a bunch of whining about one thing that you could possibly repair when you werent too busy in search of attention.

    Reply
  15. moncler jackets Posted On

    I’m typically to blogging and i actually respect your content. The article has really peaks my interest. I am going to bookmark your site and maintain checking for brand spanking new information.

    Reply
  16. tom ford sunglasses Posted On

    I抎 should verify with you here. Which is not something I usually do! I take pleasure in studying a publish that may make folks think. Additionally, thanks for permitting me to comment!

    Reply
  17. Adidas NMD Men Women Mesh Surface Big Sun Red Posted On

    A lot of thanks for your entire efforts on this web page. Kate takes pleasure in conducting investigations and it’s simple to grasp why. Most of us notice all about the powerful manner you produce powerful tactics via this website and as well attract response from people about this subject then our favorite daughter is without question discovering a lot. Take advantage of the rest of the new year. You are conducting a very good job.

    Reply
  18. adidas yeezy boost Posted On

    Thanks so much for providing individuals with such a nice chance to discover important secrets from this site. It can be so fantastic and also full of a good time for me personally and my office colleagues to visit your web site at the least 3 times every week to study the fresh guides you have. Of course, I’m actually satisfied for the amazing knowledge served by you. Some 3 areas on this page are ultimately the best we have all ever had.

    Reply
  19. fila shoes Posted On

    The following time I learn a blog, I hope that it doesnt disappoint me as a lot as this one. I mean, I know it was my choice to read, however I really thought youd have one thing fascinating to say. All I hear is a bunch of whining about one thing that you could fix for those who werent too busy looking for attention.

    Reply
  20. michael kors handbags Posted On

    There are some interesting closing dates in this article but I don抰 know if I see all of them middle to heart. There is some validity but I’ll take maintain opinion till I look into it further. Good article , thanks and we want more! Added to FeedBurner as well

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *