Module: Apriori

Included in:
Adapter
Defined in:
lib/apriori.rb,
lib/apriori/adapter.rb,
lib/apriori/version.rb,
lib/apriori/association_rule.rb,
ext/Apriori.c

Defined Under Namespace

Modules: VERSION Classes: Adapter, AssociationRule

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_association_rules(input, opts = {}) ⇒ Object

Find association rules. Given input and opts returns an Array of AssociationRules. See README.txt if you are unsure why you would want to do this.

input can be an Array of Array’s of String objects or a String specifing a path a transactions file.

The options are:

  • :min_items: minimal number of items per rule (default: 1)

  • :max_items: maximal number of items per rule (default: no limit)

  • :min_support: minimal support of a rule (default: 10 (percent %))

  • :max_support: maximal support of a rule (default: 100 (percent %))

  • :min_confidence: minimal confidence of a rule (default: 80 (percent %))

  • :output_file: write the rules to this file instead of returning AssociationRule objects. If this option is specified the path to this file is returned

Examples:

This first example passes in an Array of Arrays of Strings. The idea is that each individual Array of Strings is a transaction and the containing Array is the set of all transactions.

In this example, we call #find_association_rules with the default options.

transactions = [ %w{beer doritos},
                 %w{apple cheese},
                 %w{apple cheese},
                 %w{apple doritos} ]

rules = Apriori.find_association_rules(transactions)

In this example we read the transactions from a file. The format of the file is one transaction per line, space separated items. For instance:

# save to /path/to/some/file.txt
beer doritos
apple cheese
apple cheese
apple doritos

Here is how to call it, using many options:

rules = Apriori.find_association_rules("/path/to/some/file.txt",
                 :min_items => 2,
                 :max_items => 2,
                 :min_support => 0.01, 
                 :max_support => 100, 
                 :min_confidence => 20)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/apriori.rb', line 81

def self.find_association_rules(input, opts={})
  args = []
  
  # create the input file
  if input.kind_of?(String)
    args << input
  elsif input.kind_of?(Array)
    tempfile = create_temporary_file_from_transactions(input)
    args << tempfile.path
  else
    raise "unknown input"
  end

  # create an output file somewhere
  output_file = nil
  if opts[:output_file]
    output_file = opts[:output_file] 
  else
    tempfile = Tempfile.new("transactions_results_#{$!}_#{rand.to_s}")
    tempfile.close # starts open
    output_file = tempfile.path
  end
  args << output_file

  args << "-m#{opts[:min_items]}"      if opts[:min_items]
  args << "-n#{opts[:max_items]}"      if opts[:max_items]
  args << "-s#{opts[:min_support]}"    if opts[:min_support]
  args << "-S#{opts[:max_support]}"    if opts[:max_support]
  args << "-c#{opts[:min_confidence]}" if opts[:min_confidence]

  args << "-a"

  adapter = Adapter.new
  adapter.call_apriori_with_arguments(args)

  if opts[:output_file]
    return output_file
  else
    return AssociationRule.from_file(output_file)
  end
end

Instance Method Details

#do_aprioriObject

VALUE method_find_association_rules(VALUE self, VALUE filein, VALUE fileout, VALUE opts);



17
# File 'ext/Apriori.c', line 17

VALUE method_ap_do_apriori(VALUE self, VALUE rargv);

#do_test_aprioriObject

Prototype for our method ‘test1’ - methods are prefixed by ‘method_’ here



13
# File 'ext/Apriori.c', line 13

VALUE method_do_test_apriori(VALUE self);