Class: DecisionTree
- Inherits:
-
Object
- Object
- DecisionTree
- Defined in:
- lib/decision-tree.rb
Instance Method Summary collapse
- #feature_index ⇒ Object
- #feature_name ⇒ Object
-
#initialize(entries, columns = nil, algorithm = 'c45', dimension = nil, parent_node = nil, threshold = nil, path = nil) ⇒ DecisionTree
constructor
A new instance of DecisionTree.
- #to_pseudo_code(buff = nil, indent = "") ⇒ Object
Constructor Details
#initialize(entries, columns = nil, algorithm = 'c45', dimension = nil, parent_node = nil, threshold = nil, path = nil) ⇒ DecisionTree
Returns a new instance of DecisionTree.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/decision-tree.rb', line 30 def initialize(entries, columns=nil, algorithm='c45', dimension=nil, parent_node=nil, threshold=nil, path=nil) @parent_node = parent_node @path = if path.nil? Array.new else path end @threshold = threshold @algorithm = if algorithm=='c45' or algorithm=='id3' algorithm else raise "Unknown algorithm" end @dimension = if dimension.nil? entries[0][:features].size else dimension end @columns = if columns.nil? @dimension.times.map{|i| "feature_#{i}"} elsif columns.size != @dimension raise "The number of columns is incorrect" else columns end @labels = entries.map{|x| x[:label]} @entropy = @labels.entropy @child_nodes = Hash.new return if @path.size == @dimension return if @entropy==0.0 @path << choose_best_feature(entries) build_child_nodes(entries) end |
Instance Method Details
#feature_index ⇒ Object
74 75 76 |
# File 'lib/decision-tree.rb', line 74 def feature_index @path[-1] end |
#feature_name ⇒ Object
79 80 81 |
# File 'lib/decision-tree.rb', line 79 def feature_name @columns[ @path[-1] ] end |
#to_pseudo_code(buff = nil, indent = "") ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/decision-tree.rb', line 83 def to_pseudo_code(buff=nil,indent="") buff = Array.new if buff.nil? if @child_nodes.size==0 result = @labels.to_set.to_a if result.size==1 buff << "#{indent}return #{result[0]}" else buff << "#{indent}return #{@labels}" end end @child_nodes.each do |feature_value,child_node| buff << "#{indent}if(#{feature_name} == #{feature_value}){" child_node.to_pseudo_code(buff, indent+" " ) buff << "#{indent}}" end return buff end |