class FpGrowth::FpTree::Builder::FirstPass

Attributes

supports[RW]

Public Class Methods

new(threshold=1) click to toggle source
# File lib/fpgrowth/fp_tree/builder/first_pass.rb, line 8
def initialize(threshold=1)

  @supports = Hash.new 0
  @threshold = threshold

end

Public Instance Methods

execute(transactions, threshold=@threshold) click to toggle source

Actually make the first pass

# 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) click to toggle source

discard unfrequent items @param supports Hash

# 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) click to toggle source

Scan data and find support for each item @param transactions FpGrowth::Transaction

# 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) click to toggle source

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

# File lib/fpgrowth/fp_tree/builder/first_pass.rb, line 49
def sort(supports=@supports)
  Hash[(supports.sort_by { |_key, value| value }.reverse)]
end