Skip to content

Mananp96/Email-Spam-Detector-Naive-Bayes-Classifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Email-Spam-Detector-Naive-Bayes-Classifier

A Python-based spam detector using the Naive Bayes approach.

The spam detection process goes through a series of following steps:

1. Common Aspects of Text Mining:

  • Preparation of Corpus.
  • Corpus Inspection.
  • Cleansing of Corpus.
  • Tokenize the Corpus and count the word frequency.
  • Compute probabilities.

2. Naive Bayes Classifier Approach:

  • Build the Vocabulary of words by separating SPAM and HAM from training data.
  • Store vocabulary of words in a file.
  • Training the classifier on vocabulary.
  • Evaluate the performance of model on test data.
  • Generate Confusion and Evaluation Matrix.

File Structure:

.
├── Processor.py	# File Processor and Text Processor
├── Model.py		# Naive Bayes Classifier
├── main.py		# Entry point of script
├── dataset
	├── train		# Training dataset
	├── test	        # Test dataset
├── results      		
	├── model.txt		# Saved Model
	├── result.txt  	# Classification Results
└── README.md

Processor.py

This file contains code for reading and loading the training and test documents of spam and ham class. It also consists of logic necessary to implement the steps required for classification of email such as tokenization of corpus, calculating frequency of words, computing conditional probabilities, building and storing the vocabulary and classification results.

Model.py

This python file consists of below functionalities:

  1. Methods to implement the NaiveBayes Classifier as well as calculates the parameters such as Accuracy, Precision, Recall and F1 Measure required for analysis of Model.

  2. Logic that predicts class of Email as SPAM or HAM for given document.

  3. Method for constructing the Confusion Matrix.

main.py

It instantiates the objects to call methods for corpus preparation and training classifier. It also runs classifier on test data and displays the Performance Measures for the built model.

Run Code

On the Command Prompt, run the command:

python main.py

Evaluation Matrix

  • Accuracy:
  • Recall :
  • Precision:
  • F1-Measure:

Confusion Matrix

Considering SPAM as a positive class and HAM as the negative class:

SPAM (Predicted) HAM (Predicted)
SPAM (Actual) TP = 336 FN = 64
HAM (Actual) FP = 6 TN = 394

Accuracy: 0.9125

Precision: 0.9824561403508771

Recall: 0.84

f1-measure: 0.9056603773584906

Releases

No releases published

Packages

No packages published

Languages