class Scoruby::Models::DecisionTree

Attributes

root[R]

Public Class Methods

new(tree_xml) click to toggle source
# File lib/scoruby/models/decision_tree.rb, line 10
def initialize(tree_xml)
  @id   = tree_xml.attribute('id')
  @root = Node.new(tree_xml.at_xpath('TreeModel/Node'))
end

Public Instance Methods

decide(features) click to toggle source
# File lib/scoruby/models/decision_tree.rb, line 15
def decide(features)
  curr = @root
  while curr.children[0]
    prev = curr
    curr = step(curr, features)
    return if didnt_step?(curr, prev)
  end

  curr.decision
end

Private Instance Methods

didnt_step?(curr, prev) click to toggle source
# File lib/scoruby/models/decision_tree.rb, line 34
def didnt_step?(curr, prev)
  return false if prev.pred != curr.pred
  feature = curr.children[0].pred.field
  Scoruby.logger.error "Null tree: #{@id}, bad feature: #{feature}"
  true
end
step(curr, features) click to toggle source
# File lib/scoruby/models/decision_tree.rb, line 28
def step(curr, features)
  return curr unless curr.children
  next_step = curr.children.find { |c| c && c.true?(features) }
  next_step || curr
end