Skip to content

glondon/python-naive-bayes-spam-classifier

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

###Basic Naive Bayes Classifier in Python

Project is written with Python 2.7.

This approach makes use of pre-labeled data provided by the Kaggle Classroom spam detection challenge.

For setup create a virtualenv with the requirements:

virtualenv nbenv
source nbenv/bin/activate
pip install -r pathway/to/naive-baves/requirements.txt

To run the Naive Bayes classifier:

cd naive-bayes
python spam_detector.py

You can have the detector either train and evaluate itself against the training data (using 90% of the pre-labeled data as training and 10% to label) with:

detector.train_and_evaluate()

Or you can train against the entire labeled data set (2500 emails) and classify on the unlabeled data (1827 emails).

detector.train()
detector.classify(1827)  # Number of emails to classify

Ham has a label of 1 while Spam has a label of 0.

###How Naive Bayes Implemented

This solution makes use of Python's 2.7 Decimal module, which is used for floating point arithmetic. (Prevents floating point underflow!)

Inside the NaiveBayes#train method each document has common stop words removed using [NLTK](install http://www.nltk.org/install.html). The words have not yet been stemmed as this is a forthcoming feature.

Only the corpus of words are used as selectors to determine if an email is spam or ham.

To prevent words with 0 frequency from miscontruing the results, Laplace smoothing is applied to increment each 0 frequency word to 1.

About

A project I said I would take a look at and troubleshoot for a Facebook friend.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 96.3%
  • XSLT 2.9%
  • Other 0.8%