View on GitHub

Python-bayes-redis

A Redis backend Naive Bayes Classifier in Python

Download this project as a .zip file Download this project as a tar.gz file

Simple Naive Bayes Classifier - Python

Naive Bayes Classifier is an algorithm to classify texts into sets that is always learning.

The most obvious practical use of it is for Email Spam/Ham Detection.

Motivation

There is a really good video piece in Youtube here by Peter Norvig, Director of Research at Google. He spoke about The Unreasonable Effectiveness of Data.

Another great piece about the algorithm explained in plain English is by Alexander Nedelcu with his blog post here.

Cython

Parts of the code where it is applicable are optimized to leverage Cython Static Typing. The speed bump by doing so are notably significant. Please take a look below for benchmark timings.

How To Develop

If you are cloning the source code from Github or by downloading from PyPi, please have a look at BayesRedis/__init__.pyx for the main source file.

Implementation

Before meddling with Python, I translated Alexander's implementation in Ruby to PHP available here.

Benchmarking my oven fresh PHP implementation at the time, Redis was the only answer to achieve sub-second results. I tried MySQL and MongoDB before Redis.

External Dependencies

Installation and Configuration

$ sudo pip install bayesredis

Expecting Redis is installed locally:

from BayesRedis import Classifier

bayes = Classifier({
    'host': '127.0.0.1',
    'port': 6379,
    'db': 0
})

The 2 main methods are classify and train like so:

bayes.train('block of text', 'set');
bayes.classify('query')

Use Examples

Please take a look at test.py for executing the classifier.

To import data using MySQL, take a look at test-import-mysql.py.

Performance

The gear and spec used to test performance is below:

The data sets is as below:

Classifying Time: