Giter Site home page Giter Site logo

spam_classifier's Introduction

Email Classification

In the first part of the project we have implemented the spam classifier using the Naive Bayes classifier from scratch. Our objective was to understand the functioning of naive bayes classifier in the classification of emails throughly.

In the second part of the project we have implemented the spam classifier using naive bayes classifier from the scikit learn library.

We have collected the data set from SpamAssassin public mail corpus containing around 1900 spam emails and 3900 legit emails.

Spam classification using Naive Bayes

Architecture of spam classifier

An Email body is divided into three parts header, subject line and body where header contains the information of the sender, subject contains the subject of the email and body contains the actual content of the email. Before the email can be classified by a filter, preprocessing of the email is required to get the desired features.

Preprocessing the Emails

  1. The body of the emails are extracted from the email.
  2. A pandas data frame is created having the email bodies and category.
  3. Morphological analysis of the messages in the data set is done. After the morphological analysis each message body is converted into the individual words. We have used the natural language tool kit (nltk) for this purpose. It involves:
    • Removing the HTML Tags from the body of the email.
    • Stemming of the words.
    • Removal of stop words.
  4. A pandas series is created having all the words in all the emails.
  5. A vocabulary is created with the most frequent 2500 words.
  6. A feature matrix is created having entry for each word in the email.
  7. Data is split into 70% training and 30% test set.
  8. A sparse matrix is created for the training and the testing data.

Bayes Theorem

We assume that each word in an email is independent of every other word an hence the name Naive Bayes.

If a word is present in an email we can write the probability of the email being spam given that it contains a particular word as: and the email being ham as:

For all the words in our vocabulary, we will find , the probability of a given word and , the probability of the word in spam emails. We will also calculate the probability of spam emails in training data as as:

Training the Naive Bayes Classifier

  1. Calculate , the probability of spam emails.
  2. For all the words in the dictionary calculate

Let's say an email contains words X and Y. The probability of the email being spam is:

The probability of the email being ham is:

For prediction we can remove the term from the denominator of our expression because it is common in both cases.

If then the email will be classified as spam else it will be classified as non-spam or ham.

Testing the Naive Bayes Classifier

  1. Set the Priori as
  2. Calculate the probability of an email being spam and non-spam by taking product of the test data and probabilities of individual words and multiply the priori to the result.
  3. An email will be classified comparing the two probabilities.

Results

Confusion Matrix for 1722 emails in test set

Predicted Spam Predicted Ham
Spam True Positives = 557 False Negatives = 30
Ham False Positives = 15 True Negatives = 1120
  • Accuracy = = 97.39%
  • Recall score = = 94.89%
  • Precision score = = 97.37%
  • F score = = 96%



Spam classification using scikit learn

Implementation of spam classifier using the naive bayes classifier from the scikit learn library. The vocabulary was generated using CountVectorizer from the sklearn library instead of generating the features manually.

Results

  • Accuracy
  • Recall
  • Precision
  • F1 score

spam_classifier's People

Contributors

anish-jangra avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.