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.



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

def initialize(threshold=1)

  @supports = Hash.new 0
  @threshold = threshold

end

Instance Attribute Details

#supportsObject

Returns the value of attribute supports.



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

def supports
  @supports
end

Instance Method Details

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

Actually make the first pass



61
62
63
64
65
66
67
# File 'lib/fpgrowth/fp_tree/builder/first_pass.rb', line 61

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fpgrowth/fp_tree/builder/first_pass.rb', line 36

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

  minimum = transactions.size.to_f / 100 * threshold

  for transaction in transactions
    for item in transaction

      transaction.delete(item) if supports[item] < minimum
    end
  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



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

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



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

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