class Scoruby::Models::RandomForest::Model

Public Class Methods

new(xml) click to toggle source
# File lib/scoruby/models/random_forest/model.rb, line 14
def initialize(xml)
  @data = Data.new(xml)
end

Public Instance Methods

decisions_count(features) click to toggle source
# File lib/scoruby/models/random_forest/model.rb, line 27
def decisions_count(features)
  formatted_features = Features.new(features).formatted
  decisions = traverse_trees(formatted_features)
  aggregate_decisions(decisions)
end
score(features) click to toggle source
# File lib/scoruby/models/random_forest/model.rb, line 18
def score(features)
  decisions_count = decisions_count(features)
  decision = decisions_count.max_by { |_, v| v }
  {
    label: decision[0],
    score: decision[1] / decisions_count.values.reduce(0, :+).to_f
  }
end

Private Instance Methods

aggregate_decisions(decisions) click to toggle source
# File lib/scoruby/models/random_forest/model.rb, line 41
def aggregate_decisions(decisions)
  decisions.each_with_object(Hash.new(0)) do |score, counts|
    counts[score] += 1
  end
end
traverse_trees(formatted_features) click to toggle source
# File lib/scoruby/models/random_forest/model.rb, line 35
def traverse_trees(formatted_features)
  decision_trees.map do |decision_tree|
    decision_tree.decide(formatted_features).score
  end
end