Class: HybridForest::Trees::MultiwayNode

Inherits:
Object
  • Object
show all
Defined in:
lib/hybridforest/trees/nodes/multiway_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_classObject (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

#pathsObject (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

#branchesObject



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


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

#testsObject



13
14
15
# File 'lib/hybridforest/trees/nodes/multiway_node.rb', line 13

def tests
  @paths.keys
end