Multi-variate Bayesian classification engine
Latest commit 9bd1d03 Apr 16, 2013 @jmdeldin jmdeldin Merge pull request #4 from jmdeldin/move-devel-to-19
Update development environment for 1.9


BayesMotel is a multi-variate Bayesian classification engine. There are two steps to Bayesian classification:

  1. Training You provide a set of variables along with the proper classification for that set.
  2. Runtime You provide a set of variables and ask for the proper classification according to the training in Step 1.

Commonly this is used for spam detection. You will provide a corpus of emails or other data along with a "Spam/NotSpam" classification. The library will determine which variables affect the classification and use that to judge future data.


Step one is to create a corpus that you can train with a set of previously classified documents:

corpse ='tweets')
spam_tweets.each do |tweet|
  corpse.train(tweet, :spam)
good_tweets.each do |tweet|
  corpse.train(tweet, :ham)

In this example, we have a set of spammy tweets and a set of known good tweets. We pass in each tweet to our train() method. Once we have completed training, we call cleanup which will run through the internal data structures and clean up any variables that are too 'unique' to make a difference in classification (for instance, an :id variable will be unique for each tweet and so will be removed in the cleanup since it does not repeat enough times).

Step two is to use the calculated corpus for the category scores or a classification for a given document:

=> { :spam => 12.4, :ham => 15.25 }
=> [:ham, 15.25]


Bates Motel is the motel in Alfred Hitchcock's masterpiece Psycho. Corpus is Latin for "body" but also means 'a canonical set of documents'. I'm not crazy, I just like puns.


Mike Perham, mperham AT, @mperham,


Copyright (c) 2010 Mike Perham. See LICENSE for details.