Skip to content

Instantly share code, notes, and snippets.

@kirel
Created September 20, 2009 15:03
Show Gist options
  • Select an option

  • Save kirel/189817 to your computer and use it in GitHub Desktop.

Select an option

Save kirel/189817 to your computer and use it in GitHub Desktop.
require 'sample'
class DecisionTree
def initialize deciders
@deciders = deciders
@tree = {}
end
def << sample # TODO should be able to add to multiple subtrees
t = @tree
@deciders[0..-2].each do |d|
branch = d.call sample.data
t[branch] ||= {}
t = t[branch]
end
branch = @deciders.last.call sample.data
t[branch] ||= []
t[branch] << sample
end
# prune samples and return a smaller subset
def call data
t = @tree
@deciders[0..-2].each do |d|
branch = d.call data
t = t[branch]
raise 'Can not classify' if t.nil?
end
branch = @deciders.last.call data
t[branch] || raise('Can not classify')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment