Apriori.rb – a gem to find regularities in buying behavior (frequent itemsets)

What it is

Apriori.rb is a library to efficiently find item association rules within large sets of transactions. The goal is to find regularities in buying behavior. This library provides a thin ruby wrapper around Christian Borgelt’s C implementation of this algorithm.

Why use it?

Finding items that are frequently purchased together can be very useful in learning how to position products in your store or website. If you find that customers who buy product a also buy product b then you know to put a and b close together, thus increasing revenue.

An association rule is something like:

If a customer buys wine he buys cheese too.

How to use it

  require 'apriori'
  transactions = [  %w{beer doritos},
                    %w{apple cheese}, 
                    %w{beer doritos}, 
                    %w{apple cheese}, 
                    %w{apple cheese}, 
                    %w{apple doritos} ]
  rules = Apriori.find_association_rules(transactions,
                            :min_items => 2,
                            :max_items => 5,
                            :min_support => 1, 
                            :max_support => 100, 
                            :min_confidence => 20)
  puts rules.join("\n")
  # Results: 
  # beer -> doritos (33.3/2, 100.0)
  # doritos -> beer (50.0/3, 66.7)
  # doritos -> apple (50.0/3, 33.3)
  # apple -> doritos (66.7/4, 25.0)
  # cheese -> apple (50.0/3, 100.0)
  # apple -> cheese (66.7/4, 75.0)
  # NOTE:
  # beer -> doritos (33.3/2, 100.0)
  # means: 
  # * beer appears in 33.3% (2 total) of the transactions (the support)
  # * beer implies doritos 100% of the time (the confidence)

More information and code samples available in the documentation (rdoc).

How to get it

Install the gem:

gem install apriori

Get the source:

git clone git://github.com/jashmenn/apriori.git

Or view the source on github.


requires rubygems >= 1.2.0 . This is a requirement that hopefully will be changed in the future. But for now, know that if you don’t have rubygems >= 1.2.0 it will fail silently on the install. To update your rubygems version do:

gem update --system

  • del.icio.us
  • Reddit
  • Technorati
  • Twitter
  • Facebook
  • Google Bookmarks
  • HackerNews
  • PDF
  • RSS
This entry was posted in ruby and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.