Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #8 from RobinLassonde/master
Browse files Browse the repository at this point in the history
Rescue error that is raised when ID3 prediction fails on a row of data
  • Loading branch information
SergioFierens committed Dec 19, 2012
2 parents a8f8168 + c95f000 commit 7575d32
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/ai4r/classifiers/id3.rb
Expand Up @@ -280,7 +280,7 @@ def initialize(data_labels, index, values, nodes)


def value(data) def value(data)
value = data[@index] value = data[@index]
return rule_not_found if !@values.include?(value) return ErrorNode.new.value(data) if !@values.include?(value)
return nodes[@values.index(value)].value(data) return nodes[@values.index(value)].value(data)
end end


Expand Down Expand Up @@ -313,9 +313,13 @@ def get_rules
end end
end end


class ModelFailureError < StandardError
default_message = "There was not enough information during training to do a proper induction for this data element."
end

class ErrorNode #:nodoc: all class ErrorNode #:nodoc: all
def value(data) def value(data)
raise "There was not enough information during training to do a proper induction for this data element." raise ModelFailureError, "There was not enough information during training to do a proper induction for the data element #{data}."
end end
def get_rules def get_rules
return [] return []
Expand Down
12 changes: 12 additions & 0 deletions test/classifiers/id3_test.rb
Expand Up @@ -203,6 +203,18 @@ def test_rules_eval
eval id3.get_rules eval id3.get_rules
assert_equal 'N', marketing_target assert_equal 'N', marketing_target
end end

def test_model_failure
bad_data_items = [ ['a', 'Y'],
['b', 'N'],
]
bad_data_labels = ['bogus', 'target']
id3 = ID3.new.build(DataSet.new(:data_items =>bad_data_items, :data_labels => bad_data_labels))
assert_raise ModelFailureError do
id3.eval(['c'])
end
assert_equal true, true
end
end end




0 comments on commit 7575d32

Please sign in to comment.