Class: HybridForest::Trees::MultiwayNode
- Inherits:
-
Object
- Object
- HybridForest::Trees::MultiwayNode
- Defined in:
- lib/hybridforest/trees/nodes/multiway_node.rb
Instance Attribute Summary collapse
-
#majority_class ⇒ Object
readonly
Returns the value of attribute majority_class.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
Instance Method Summary collapse
- #branch_for(instance) ⇒ Object
- #branches ⇒ Object
- #classify(instance) ⇒ Object
-
#initialize(paths, instances) ⇒ MultiwayNode
constructor
A new instance of MultiwayNode.
- #majority_vote(instances) ⇒ Object
- #print_string(spacing = "") ⇒ Object
- #tests ⇒ Object
Constructor Details
#initialize(paths, instances) ⇒ MultiwayNode
Returns a new instance of MultiwayNode.
8 9 10 11 |
# File 'lib/hybridforest/trees/nodes/multiway_node.rb', line 8 def initialize(paths, instances) @paths = paths @majority_class = majority_vote(instances) end |
Instance Attribute Details
#majority_class ⇒ Object (readonly)
Returns the value of attribute majority_class.
6 7 8 |
# File 'lib/hybridforest/trees/nodes/multiway_node.rb', line 6 def majority_class @majority_class end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
6 7 8 |
# File 'lib/hybridforest/trees/nodes/multiway_node.rb', line 6 def paths @paths end |
Instance Method Details
#branch_for(instance) ⇒ Object
21 22 23 24 |
# File 'lib/hybridforest/trees/nodes/multiway_node.rb', line 21 def branch_for(instance) match = tests.find { |test| test.passed_by? instance } paths[match] end |
#branches ⇒ Object
17 18 19 |
# File 'lib/hybridforest/trees/nodes/multiway_node.rb', line 17 def branches @paths.values end |
#classify(instance) ⇒ Object
26 27 28 29 |
# File 'lib/hybridforest/trees/nodes/multiway_node.rb', line 26 def classify(instance) branch = branch_for instance branch&.classify(instance) || @majority_class end |
#majority_vote(instances) ⇒ Object
39 40 41 42 |
# File 'lib/hybridforest/trees/nodes/multiway_node.rb', line 39 def majority_vote(instances) labels = instances[instances.label].to_enum labels.max_by(1) { |label| labels.count(label) }.first end |
#print_string(spacing = "") ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/hybridforest/trees/nodes/multiway_node.rb', line 31 def print_string(spacing = "") paths.each do |test, branch| print spacing puts "#{test} True" branch.print_string(spacing + " ") end end |
#tests ⇒ Object
13 14 15 |
# File 'lib/hybridforest/trees/nodes/multiway_node.rb', line 13 def tests @paths.keys end |