Class: Apriori::AssociationRule

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#antecedentObject

Returns the value of attribute antecedent.



17
18
19
# File 'lib/apriori/association_rule.rb', line 17

def antecedent
  @antecedent
end

#confidenceObject

Returns the value of attribute confidence.



22
23
24
# File 'lib/apriori/association_rule.rb', line 22

def confidence
  @confidence
end

#consequentObject

Returns the value of attribute consequent.



21
22
23
# File 'lib/apriori/association_rule.rb', line 21

def consequent
  @consequent
end

#num_antecedent_transactionsObject

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

#supportObject

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 Itemsets. 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)


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/apriori/association_rule.rb', line 44

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 AssociationRules



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/apriori/association_rule.rb', line 75

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:

Returns:

  • (Boolean)


70
71
72
# File 'lib/apriori/association_rule.rb', line 70

def eql?(object) #:nodoc:
  self == (object)
end

#to_sObject

Returns the standard form of this rule as a string. For instance:

bar baz bangle -> foo (66.7/4, 75.0)


59
60
61
62
63
64
65
66
67
68
# File 'lib/apriori/association_rule.rb', line 59

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