Class: Apriori::AssociationRule
- Inherits:
-
Object
- Object
- Apriori::AssociationRule
- Defined in:
- lib/apriori/association_rule.rb
Overview
This class represents a single association rule.
From Christian’s original documentation:
An association rule is a rule like “If a customer buys wine and bread, he often buys cheese, too.”
An association rule states that if we pick a customer at random and find out that he selected certain items (bought certain products, chose certain options etc.), we can be confident, quantified by a percentage, that he also selected certain other items (bought certain other products, chose certain other options etc.).
Instance Attribute Summary collapse
-
#antecedent ⇒ Object
Returns the value of attribute antecedent.
-
#confidence ⇒ Object
Returns the value of attribute confidence.
-
#consequent ⇒ Object
Returns the value of attribute consequent.
-
#num_antecedent_transactions ⇒ Object
Returns the value of attribute num_antecedent_transactions.
-
#support ⇒ Object
Returns the value of attribute support.
Class Method Summary collapse
-
.from_file(filename) ⇒ Object
Given
filename
of a file containing itemset information returns an Array ofItemset
s. -
.parse_line(line) ⇒ Object
Given
line
returns an Itemset Example of a line: foo <- bar baz bangle (66.7/4, 75.0).
Instance Method Summary collapse
-
#==(object) ⇒ Object
Check equality between to
AssociationRule
s. -
#eql?(object) ⇒ Boolean
:nodoc:.
-
#to_s ⇒ Object
Returns the standard form of this rule as a string.
Instance Attribute Details
#antecedent ⇒ Object
Returns the value of attribute antecedent.
17 18 19 |
# File 'lib/apriori/association_rule.rb', line 17 def antecedent @antecedent end |
#confidence ⇒ Object
Returns the value of attribute confidence.
22 23 24 |
# File 'lib/apriori/association_rule.rb', line 22 def confidence @confidence end |
#consequent ⇒ Object
Returns the value of attribute consequent.
21 22 23 |
# File 'lib/apriori/association_rule.rb', line 21 def consequent @consequent end |
#num_antecedent_transactions ⇒ Object
Returns the value of attribute num_antecedent_transactions.
18 19 20 |
# File 'lib/apriori/association_rule.rb', line 18 def num_antecedent_transactions @num_antecedent_transactions end |
#support ⇒ Object
Returns the value of attribute support.
19 20 21 |
# File 'lib/apriori/association_rule.rb', line 19 def support @support end |
Class Method Details
.from_file(filename) ⇒ Object
Given filename
of a file containing itemset information returns an Array of Itemset
s. File format must match that of #parse_line.
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/apriori/association_rule.rb', line 27 def from_file(filename) rules = [] begin contents = File.read(filename) contents.each_line do |line| rules << parse_line(line) end rescue => e puts "Error reading: #{filename}" puts e end rules end |
.parse_line(line) ⇒ Object
Given line
returns an Itemset Example of a line:
foo <- bar baz bangle (66.7/4, 75.0)
Note that this is the opposite order of how apriori.rb returns the AssociationRule#to_s. (apriori.rb returns the antecedent on the left and consequent on the right)
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/apriori/association_rule.rb', line 48 def parse_line(line) is = new line =~ /(.+)\s+<-\s+(.+?)\s+\((\d+\.\d)(?:\/(\d+))?,\s+(\d+\.\d)\)/ consequent, antecedent, support, transactions, confidence = $1, $2, $3, $4, $5 is.consequent = consequent is.antecedent = antecedent.split(/\s+/) is.support = support.to_f is.num_antecedent_transactions = transactions ? transactions.to_i : nil is.confidence = confidence.to_f is end |
Instance Method Details
#==(object) ⇒ Object
Check equality between to AssociationRule
s
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/apriori/association_rule.rb', line 83 def ==(object) return true if object.equal?(self) if object.instance_of?(self.class) %w{antecedent num_antecedent_transactions support consequent confidence}.each do |key| return false unless object.send(key) == self.send(key) end return true else return false end end |
#eql?(object) ⇒ Boolean
:nodoc:
78 79 80 |
# File 'lib/apriori/association_rule.rb', line 78 def eql?(object) #:nodoc: self == (object) end |
#to_s ⇒ Object
Returns the standard form of this rule as a string. For instance:
bar baz bangle -> foo (66.7/4, 75.0)
# (antecedent) (consequent)
Note that this order is the opposite order of the association rules returned by apriori.c I believe this format reads more naturally.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/apriori/association_rule.rb', line 67 def to_s # "%s <- %s (%0.01f%s, %0.01f)" % [ consequent, # antecedent.join(" "), # support, # num_antecedent_transactions ? "/#{num_antecedent_transactions}" : "", confidence ] "%s -> %s (%0.01f%s, %0.01f)" % [ antecedent.join(" "), consequent, support, num_antecedent_transactions ? "/#{num_antecedent_transactions}" : "", confidence ] end |