{"id":22193,"date":"2021-09-25T07:17:30","date_gmt":"2021-09-25T07:17:30","guid":{"rendered":"https:\/\/www.askpython.com\/?p=22193"},"modified":"2021-10-01T19:41:08","modified_gmt":"2021-10-01T19:41:08","slug":"email-spam-classification","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/email-spam-classification","title":{"rendered":"Email Spam Classification in Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hello fellow learner! In this tutorial, we will talk about how to achieve the classification of spam emails with the help of the dataset which will be loaded using scikit-learn in Python programming language.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Email Spam<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We all know that billions of spam are sent every day to user email accounts and more than 90% of these spam emails are malicious and cause major harm to the user. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Don\u2019t the spams get annoying to you as well? They get pretty annoying to me for sure! Sometimes even some important mails get transferred to spam and as a result, some important information is left unread with the fear of getting harmed by the spam emails.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And did you know that <strong>one out of every 1,000 e-mails contains malware charges<\/strong>? And hence it is important for us to learn how can we ourselves classify our emails as safe and unsafe.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Implementing Email Spam Classifier in Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s get right into the steps to implement an email spam classification algorithm using Python. This will help you understand the backend working of a very basic spam classifier. The algorithms used in the real world are way more advanced compared to the algorithm I&#8217;ve described below. But you sure can use this as a starting point for your journey.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Importing Modules and Loading Data<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, we import all the necessary required modules into our program. The code for the same is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.naive_bayes import MultinomialNB, GaussianNB\nfrom sklearn import svm\nfrom sklearn.model_selection import GridSearchCV\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We would require some basic machine learning modules such as <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-bitwise-operations\" data-type=\"post\" data-id=\"13577\">numpy<\/a>, <a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/python-pandas-module-tutorial\" data-type=\"post\" data-id=\"2986\">pandas<\/a>, and <a href=\"https:\/\/www.askpython.com\/python-modules\/matplotlib\/python-matplotlib\" data-type=\"post\" data-id=\"3182\">matplotlib<\/a>. Along with these, we would require some <code>sklearn<\/code> models and features.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The next step involves loading the dataset with the help of the pandas module imported earlier. The dataset we would be using is the <code>spam.csv<\/code> data file which can be found <a href=\"https:\/\/www.kaggle.com\/uciml\/sms-spam-collection-dataset\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.kaggle.com\/uciml\/sms-spam-collection-dataset\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ndata = pd.read_csv(&#039;.\/spam.csv&#039;)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The dataset we loaded has 5572 email samples along with 2 unique labels namely, <code>spam<\/code> and <code>ham<\/code>. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Training and Testing Data<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After loading we have to separate the data into <a href=\"https:\/\/www.askpython.com\/python\/examples\/split-data-training-and-testing-set\" data-type=\"post\" data-id=\"9234\">training and testing data<\/a>.\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The separation of data into training and testing data includes two steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Separating the x and y data as the email text and labels respectively<\/li><li>Splitting the x and y data into four different datasets namely x_train,y_train,x_test, and y_test based on the 80:20 rule.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The separation of data into x and y data is done in the following code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nx_data=data&#x5B;&#039;EmailText&#039;]\ny_data=data&#x5B;&#039;Label&#039;]\n\nsplit =(int)(0.8*data.shape&#x5B;0])\nx_train=x_data&#x5B;:split]\nx_test=x_data&#x5B;split:]\ny_train=y_data&#x5B;:split]\ny_test=y_data&#x5B;split:]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. Extracting Important Features<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The next step is to get only the important words\/features from the whole dataset. To achieve this, we will make use of the <code>CountVectorizer<\/code> function in order to vectorize the words of the training dataset.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ncount_vector = CountVectorizer()  \nextracted_features = count_vector.fit_transform(x_train)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">4. Building and Training The Model<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The most important step involves building and training the model for the dataset we created earlier. The code for the same is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ntuned_parameters = {&#039;kernel&#039;: &#x5B;&#039;rbf&#039;,&#039;linear&#039;], &#039;gamma&#039;: &#x5B;1e-3, 1e-4],&#039;C&#039;: &#x5B;1, 10, 100, 1000]}\nmodel = GridSearchCV(svm.SVC(), tuned_parameters)\nmodel.fit(extracted_features,y_train)\n\nprint(&quot;Model Trained Successfully!&quot;)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The final step includes computing the overall accuracy of our model on the testing dataset.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nprint(&quot;Accuracy of the model is: &quot;,model.score(count_vector.transform(x_test),y_test)*100)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We ended up achieving an accuracy of &nbsp;<code><b>98.744%<\/b><\/code><strong> <\/strong>&nbsp;which is great!!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Implementing an email classification system is a great next step in developing the technology and making emails more secure. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hope you loved the tutorial! Happy Learning! &#x1f607;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Also Read:<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/handwritten-digit-recognition\">Handwritten Digit Recognition in Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/image-segmentation\">Python: Image Segmentation<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/spell-checker-in-python\">Spell Checker in Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/k-nearest-neighbors-from-scratch\">K-Nearest Neighbors from Scratch with Python<\/a><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello fellow learner! In this tutorial, we will talk about how to achieve the classification of spam emails with the help of the dataset which will be loaded using scikit-learn in Python programming language. Introduction to Email Spam We all know that billions of spam are sent every day to user email accounts and more [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":22198,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-22193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/22193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=22193"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/22193\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/22198"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=22193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=22193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=22193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}