Class: Ai4r::Classifiers::EvaluationNode

Inherits:
Object
  • Object
show all
Defined in:
lib/ai4r/classifiers/id3.rb

Overview

:nodoc: all

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_labels, index, values_or_threshold, nodes, numeric = false, majority = nil) ⇒ Object

Parameters:

  • data_labels (Object)
  • index (Object)
  • values_or_threshold (Object)
  • nodes (Object)
  • numeric (Object) (defaults to: false)
  • majority (Object) (defaults to: nil)


553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/ai4r/classifiers/id3.rb', line 553

def initialize(data_labels, index, values_or_threshold, nodes, numeric = false,
               majority = nil)
  @index = index
  @numeric = numeric
  if numeric
    @threshold = values_or_threshold
    @values = nil
  else
    @values = values_or_threshold
  end
  @nodes = nodes
  @majority = majority
  @data_labels = data_labels
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



544
545
546
# File 'lib/ai4r/classifiers/id3.rb', line 544

def index
  @index
end

#majorityObject (readonly)

Returns the value of attribute majority.



544
545
546
# File 'lib/ai4r/classifiers/id3.rb', line 544

def majority
  @majority
end

#nodesObject (readonly)

Returns the value of attribute nodes.



544
545
546
# File 'lib/ai4r/classifiers/id3.rb', line 544

def nodes
  @nodes
end

#numericObject (readonly)

Returns the value of attribute numeric.



544
545
546
# File 'lib/ai4r/classifiers/id3.rb', line 544

def numeric
  @numeric
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



544
545
546
# File 'lib/ai4r/classifiers/id3.rb', line 544

def threshold
  @threshold
end

#valuesObject (readonly)

Returns the value of attribute values.



544
545
546
# File 'lib/ai4r/classifiers/id3.rb', line 544

def values
  @values
end

Instance Method Details

#get_rulesObject

Returns:

  • (Object)


588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/ai4r/classifiers/id3.rb', line 588

def get_rules
  rule_set = []
  @nodes.each_with_index do |child_node, child_node_index|
    if @numeric
      op = child_node_index.zero? ? '<=' : '>'
      my_rule = "#{@data_labels[@index]} #{op} #{@threshold}"
    else
      my_rule = "#{@data_labels[@index]}=='#{@values[child_node_index]}'"
    end
    child_node_rules = child_node.get_rules
    child_node_rules.each do |child_rule|
      child_rule.unshift(my_rule)
    end
    rule_set += child_node_rules
  end
  rule_set
end

#to_graphviz(id, lines, parent = nil, edge_label = nil) ⇒ Object

Parameters:

  • id (Object)
  • lines (Object)
  • parent (Object) (defaults to: nil)
  • edge_label (Object) (defaults to: nil)

Returns:

  • (Object)


620
621
622
623
624
625
626
627
628
629
630
# File 'lib/ai4r/classifiers/id3.rb', line 620

def to_graphviz(id, lines, parent = nil, edge_label = nil)
  my_id = id
  lines << "  node#{my_id} [label=\"#{@data_labels[@index]}\"]"
  lines << "  node#{parent} -> node#{my_id} [label=\"#{edge_label}\"]" if parent
  next_id = my_id
  @nodes.each_with_index do |child, idx|
    next_id += 1
    next_id = child.to_graphviz(next_id, lines, my_id, @values[idx])
  end
  next_id
end

#to_hObject

Returns:

  • (Object)


607
608
609
610
611
612
613
# File 'lib/ai4r/classifiers/id3.rb', line 607

def to_h
  hash = {}
  @nodes.each_with_index do |child, i|
    hash[@values[i]] = child.to_h
  end
  { @data_labels[@index] => hash }
end

#value(data, classifier = nil) ⇒ Object

Parameters:

  • data (Object)
  • classifier (Object) (defaults to: nil)

Returns:

  • (Object)


571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/ai4r/classifiers/id3.rb', line 571

def value(data, classifier = nil)
  value = data[@index]
  if @numeric
    node = value <= @threshold ? @nodes[0] : @nodes[1]
    node.value(data, classifier)
  else
    unless @values.include?(value)
      return nil if classifier&.on_unknown == :nil
      return @majority if classifier&.on_unknown == :most_frequent

      return ErrorNode.new.value(data, classifier)
    end
    @nodes[@values.index(value)].value(data, classifier)
  end
end