Class: Idhja22::LeafNode

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(probability, category_label) ⇒ LeafNode

Returns a new instance of LeafNode.



151
152
153
154
# File 'lib/idhja22/tree/node.rb', line 151

def initialize(probability, category_label)
  @probability = probability
  @category_label = category_label
end

Instance Attribute Details

#category_labelObject (readonly)

Returns the value of attribute category_label.



143
144
145
# File 'lib/idhja22/tree/node.rb', line 143

def category_label
  @category_label
end

#probabilityObject (readonly)

Returns the value of attribute probability.



143
144
145
# File 'lib/idhja22/tree/node.rb', line 143

def probability
  @probability
end

Class Method Details

.build(dataset, prior) ⇒ Object



145
146
147
# File 'lib/idhja22/tree/node.rb', line 145

def build(dataset, prior)
  return new(dataset.m_estimate(prior), dataset.category_label)
end

Instance Method Details

#==(other) ⇒ Object



160
161
162
# File 'lib/idhja22/tree/node.rb', line 160

def ==(other)
  return super && self.probability == other.probability && self.category_label == other.category_label
end

#cleanup_children!Object

no-op method - a leaf node has no children by definition



174
175
176
# File 'lib/idhja22/tree/node.rb', line 174

def cleanup_children!

end

#evaluate(query) ⇒ Object



164
165
166
167
# File 'lib/idhja22/tree/node.rb', line 164

def evaluate(query)
  raise Idhja22::Dataset::Datum::UnknownCategoryLabel, "expected category label for query is #{query.category_label} but node is using #{self.category_label}" unless query.category_label == self.category_label
  return probability
end

#get_rulesObject



156
157
158
# File 'lib/idhja22/tree/node.rb', line 156

def get_rules
  ["then chance of #{category_label} = #{probability.round(2)}"]
end

#leavesObject



169
170
171
# File 'lib/idhja22/tree/node.rb', line 169

def leaves
  return [self]
end