Class: FpGrowth::FpTree::Builder::FirstPass

Inherits:
Object
  • Object
show all
Defined in:
lib/fpgrowth/fp_tree/builder/first_pass.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(threshold = 1) ⇒ FirstPass

Returns a new instance of FirstPass.



8
9
10
11
12
13
# File 'lib/fpgrowth/fp_tree/builder/first_pass.rb', line 8

def initialize(threshold=1)

  @supports = Hash.new 0
  @threshold = threshold

end

Instance Attribute Details

#supportsObject

Returns the value of attribute supports.



6
7
8
# File 'lib/fpgrowth/fp_tree/builder/first_pass.rb', line 6

def supports
  @supports
end

Instance Method Details

#execute(transactions, threshold = @threshold) ⇒ Object

Actually make the first pass



55
56
57
58
59
60
61
# File 'lib/fpgrowth/fp_tree/builder/first_pass.rb', line 55

def execute(transactions, threshold=@threshold)
  @transactions = transactions
  @threshold = threshold
  scan
  pruning
  sort
end

#pruning(transactions = @transactions, supports = @supports, threshold = @threshold) ⇒ Object

discard unfrequent items

Parameters:

  • supports (defaults to: @supports)

    Hash



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fpgrowth/fp_tree/builder/first_pass.rb', line 34

def pruning(transactions=@transactions, supports=@supports, threshold=@threshold)

  minimum = transactions.size.to_f / 100 * threshold
  for transaction in transactions
    transaction.delete_if { |item| supports[item] < minimum }
  end
  transactions.delete([])

  supports.delete_if { |key, value| value < minimum }
  return supports
end

#scan(transactions = @transactions) ⇒ Object

Scan data and find support for each item

Parameters:

  • transactions (defaults to: @transactions)

    FpGrowth::Transaction



20
21
22
23
24
25
26
27
28
29
# File 'lib/fpgrowth/fp_tree/builder/first_pass.rb', line 20

def scan(transactions=@transactions)
  @supports= Hash.new(0)
  for transaction in transactions
    for item in transaction
      @supports[item] += 1
    end

  end
  return @supports
end

#sort(supports = @supports) ⇒ Object

Ordonner les items en fonction de le support Cet ordre est utilisé pour la construction du Tree lors de la seconde passe



49
50
51
# File 'lib/fpgrowth/fp_tree/builder/first_pass.rb', line 49

def sort(supports=@supports)
  Hash[(supports.sort_by { |_key, value| value }.reverse)]
end