NaiveBayesRb

A very simple Ruby implementation of Naive Bayes classification model.

Design Considerations

  1. the interface closely resembles the python scikit-learn interface.
  2. enable model serialization and persistence, so that the model can be reused and even distributed and shared. With the default MarshalSerializer, it also allows custom serializer to be plugged in.

Usage

basics

nb = NaiveBayesRb::NaiveBayes.new
train = [[1, 20], [2, 21], [3, 22], [4, 23]] 
target = [1, 0, 1, 0] 
test = [[0, 0], [4, 24]]
predictions = nb.fit(train, target).predict(test) #=> [1, 0] 
@nb.accuracy(prediction, [1, 1]) #=> 50

Model Persistence

NaiveBayesRb::NaiveBayes.serializer =       
nb = NaiveBayesRb::NaiveBayes.new
nb.fit(train, target).save('model.pb')

Loading Persisted Model

NaiveBayesRb::NaiveBayes.serializer =       
nb = NaiveBayesRb::NaiveBayes.load('model.pb')

Installation

Add this line to your application's Gemfile:

gem 'naive_bayes_rb'

And then execute:

$ bundle

Or install it yourself as:

$ gem install naive_bayes_rb

Thanks

I followed the tutorials from this blog.