Module: FpGrowth::Helper

Defined in:
lib/fp_growth/fp_tree.rb

Class Method Summary collapse

Class Method Details

.create_assoziation_rules(frequent_patterns, min_confidence) ⇒ Object

TODO put handling of assoziation rules in an extra namespace and copy the unnecessary rules code from examples/cao.rb in it too



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fp_growth/fp_tree.rb', line 25

def self.create_assoziation_rules(frequent_patterns, min_confidence)
  result = []
  frequent_patterns.each do |pattern|
    next if pattern.size < 2
    pattern.to_a.parts.each do |part|
      other_pattern = frequent_patterns.find { |p| part.first.content_equal?(p.to_a) }
      confidence = pattern.support.to_f / other_pattern.support.to_f
      if confidence >= min_confidence
        result << {
          :left => part.first,
          :right => part.last,
          :support => pattern.support,
          :confidence => confidence
        }
      end
    end
  end
  result
end

.cross_product(a, b) ⇒ Object

takes arrays of prefix_paths



13
14
15
16
17
18
19
20
21
# File 'lib/fp_growth/fp_tree.rb', line 13

def self.cross_product(a, b)
  result = []
  a.each do |a_el|
    b.each do |b_el|
      result << (a_el + b_el)
    end
  end
  result
end

.subsets_as_prefix_path(array) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/fp_growth/fp_tree.rb', line 2

def self.subsets_as_prefix_path(array)
  result = []
  array.subsets.each do |combination|
    min_support = combination.min { |a, b| a.count <=> b.count }.count
    elements = combination.collect { |c| c.key }
    result << FpGrowth::PrefixPath.new(min_support, elements)
  end
  result
end