Class: Idhja22::DecisionNode

Inherits:
Node
  • Object
show all
Defined in:
lib/idhja22/tree/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

build_node

Constructor Details

#initialize(decision_attribute, default_probability = nil) ⇒ DecisionNode

Returns a new instance of DecisionNode.



77
78
79
80
81
# File 'lib/idhja22/tree/node.rb', line 77

def initialize(decision_attribute, default_probability = nil)
  @decision_attribute = decision_attribute
  @branches = {}
  @default_probability = (default_probability || Idhja22.config.default_probability)
end

Instance Attribute Details

#branchesObject

Returns the value of attribute branches.



55
56
57
# File 'lib/idhja22/tree/node.rb', line 55

def branches
  @branches
end

#decision_attributeObject

Returns the value of attribute decision_attribute.



55
56
57
# File 'lib/idhja22/tree/node.rb', line 55

def decision_attribute
  @decision_attribute
end

#default_probabilityObject

Returns the value of attribute default_probability.



55
56
57
# File 'lib/idhja22/tree/node.rb', line 55

def default_probability
  @default_probability
end

Class Method Details

.build(dataset, attributes_available, depth, prior = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/idhja22/tree/node.rb', line 58

def build(dataset, attributes_available, depth, prior=nil)
  data_split, best_attribute = best_attribute(dataset, attributes_available)

  probability_guess = dataset.m_estimate(prior)

  output_node = new(best_attribute, probability_guess)

  data_split.each do |value, ds|
    node = Node.build_node(ds, attributes_available-[best_attribute], depth+1, probability_guess)
    
    output_node.add_branch(value, node) if node && !(node.is_a?(DecisionNode) && node.branches.empty?)
  end

  output_node.cleanup_children!

  return output_node
end

Instance Method Details

#==(other) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/idhja22/tree/node.rb', line 100

def ==(other)
  return false unless super
  return false unless self.decision_attribute == other.decision_attribute
  return false unless self.branches.length == other.branches.length
  self.branches.each do |attr_value, node|
    return false unless other.branches.has_key?(attr_value)
    return false unless node == other.branches[attr_value]
  end
  return true
end

#add_branch(attr_value, node) ⇒ Object



83
84
85
# File 'lib/idhja22/tree/node.rb', line 83

def add_branch(attr_value, node)
  @branches[attr_value] = node
end

#category_labelObject



137
138
139
# File 'lib/idhja22/tree/node.rb', line 137

def category_label
  leaves.first.category_label
end

#cleanup_children!Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/idhja22/tree/node.rb', line 118

def cleanup_children!
  branches.each do |attr, child_node|
    child_node.cleanup_children!
    leaves = child_node.leaves
    probs = leaves.collect(&:probability)
    if(probs.max - probs.min < Idhja22.config.probability_delta)
      new_node = LeafNode.new(probs.max, category_label)
      add_branch(attr, new_node)
    end
  end
end

#evaluate(query) ⇒ Object



111
112
113
114
115
116
# File 'lib/idhja22/tree/node.rb', line 111

def evaluate(query)
  queried_value = query[self.decision_attribute]
  branch = self.branches[queried_value]
  return default_probability if branch.nil?
  branch.evaluate(query)
end

#get_rulesObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/idhja22/tree/node.rb', line 87

def get_rules
  rules = []
  branches.each do |v,n|
    current_rule = "#{decision_attribute} == #{v}"
    sub_rules = n.get_rules
    sub_rules.each do |r|
      rules << "#{current_rule} and #{r}"
    end
  end

  return rules
end

#leavesObject



130
131
132
133
134
135
# File 'lib/idhja22/tree/node.rb', line 130

def leaves
  raise Idhja22::IncompleteTree, "decision node with no branches" if branches.empty?
  branches.values.flat_map do |child_node|
    child_node.leaves
  end
end