Class: Dwarf::Classifier

Inherits:
Object
  • Object
show all
Defined in:
lib/dwarf/classifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClassifier

Returns a new instance of Classifier.



7
8
9
10
# File 'lib/dwarf/classifier.rb', line 7

def initialize()
  @examples, @example_attributes = {}, []
  @decision_tree = TreeNode.new("ROOT")
end

Instance Attribute Details

#classifier_logicObject

Returns the value of attribute classifier_logic.



5
6
7
# File 'lib/dwarf/classifier.rb', line 5

def classifier_logic
  @classifier_logic
end

#example_attributesObject

Returns the value of attribute example_attributes.



4
5
6
# File 'lib/dwarf/classifier.rb', line 4

def example_attributes
  @example_attributes
end

#examplesObject

Returns the value of attribute examples.



3
4
5
# File 'lib/dwarf/classifier.rb', line 3

def examples
  @examples
end

Instance Method Details

#add_example(example_record, classification) ⇒ Object



18
19
20
21
# File 'lib/dwarf/classifier.rb', line 18

def add_example(example_record, classification)
  @examples[example_record]=classification
  @example_attributes |= example_record.attributes
end

#add_examples(example_hash) ⇒ Object



12
13
14
15
16
# File 'lib/dwarf/classifier.rb', line 12

def add_examples(example_hash)
  example_hash.each do |example, classification|
    add_example(example, classification)
  end
end

#classify(example) ⇒ Object



23
24
25
# File 'lib/dwarf/classifier.rb', line 23

def classify(example)
  return nil
end

#learn!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dwarf/classifier.rb', line 27

def learn!
  @decision_tree.examples = @examples.keys
  pending = []
  pending.push @decision_tree
  used_attributes = []
  until pending.empty?
    node = pending.pop
    if classification = homogenous_examples(node)
      node.classification = classification
    elsif no_valuable_attributes?(node) && node.parent
      node.parent.classification= expected_value(node.examples)
    elsif no_valuable_attributes?(node)
      classifier_logic = expected_value(node.examples)
    elsif false #stub branch
      #C4.5 would also allow for previously unseen classifications
      #dwarf's API dictates all classifications are known before learning
      #starts
    else
      infogains = {}
      (@example_attributes-used_attributes).each do |example_attribute|
        infogains[information_gain(node.examples,example_attribute)] = example_attribute
      end
      best_gain = infogains.keys.sort[0]
      best_attribute = infogains[best_gain]
      split(node,best_attribute).each {|child_node| pending.push(child_node)}
      used_attributes << best_attribute
    end
  end
  self.classifier_logic = codify_tree(@decision_tree)
  implement_classify
end