Created
September 20, 2009 15:03
-
-
Save kirel/189817 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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