Class: DecisionTree
- Inherits:
-
Object
- Object
- DecisionTree
- Defined in:
- lib/decision_tree.rb
Constant Summary collapse
- ROOT =
'root'- LEFT =
'left'- RIGHT =
'right'
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #decide(features) ⇒ Object
-
#initialize(tree_xml) ⇒ DecisionTree
constructor
A new instance of DecisionTree.
- #no_true_child?(curr, prev) ⇒ Boolean
- #set_node(tree_xml, root) ⇒ Object
Constructor Details
#initialize(tree_xml) ⇒ DecisionTree
Returns a new instance of DecisionTree.
11 12 13 14 15 |
# File 'lib/decision_tree.rb', line 11 def initialize(tree_xml) @id = tree_xml.xpath('@id') @root = Tree::TreeNode.new(ROOT) set_node(tree_xml.xpath('TreeModel/Node'), @root) end |
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
9 10 11 |
# File 'lib/decision_tree.rb', line 9 def root @root end |
Instance Method Details
#decide(features) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/decision_tree.rb', line 29 def decide(features) curr = @root while curr.content.decision == '' prev = curr curr = curr[LEFT] if curr[LEFT] && curr[LEFT].content.true?(features) curr = curr[RIGHT] if curr[RIGHT] && curr[RIGHT].content.true?(features) return if no_true_child?(curr, prev) end curr.content.decision end |
#no_true_child?(curr, prev) ⇒ Boolean
42 43 44 45 46 |
# File 'lib/decision_tree.rb', line 42 def no_true_child?(curr, prev) return false if (prev.content != curr.content) RandomForester.logger.error "Null tree: #{@id}, bad feature: #{curr[LEFT].content.field }" true end |
#set_node(tree_xml, root) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/decision_tree.rb', line 17 def set_node(tree_xml, root) root.content = Predicate.new(tree_xml) return if tree_xml.xpath('*').count == 1 root << Tree::TreeNode.new(LEFT) root << Tree::TreeNode.new(RIGHT) set_node(tree_xml.xpath('*')[1], root[LEFT]) if tree_xml.xpath('*')[1] set_node(tree_xml.xpath('*')[2], root[RIGHT]) if tree_xml.xpath('*')[2] end |