Permalink
Browse files

Merge pull request #8 from RobinLassonde/master

Rescue error that is raised when ID3 prediction fails on a row of data
  • Loading branch information...
2 parents a8f8168 + c95f000 commit 7575d32afcbbb0d890ae630b5c4b254a792f178a @SergioFierens committed Dec 19, 2012
Showing with 18 additions and 2 deletions.
  1. +6 −2 lib/ai4r/classifiers/id3.rb
  2. +12 −0 test/classifiers/id3_test.rb
@@ -280,7 +280,7 @@ def initialize(data_labels, index, values, nodes)
def value(data)
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)
end
@@ -313,9 +313,13 @@ def get_rules
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
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
def get_rules
return []
@@ -203,6 +203,18 @@ def test_rules_eval
eval id3.get_rules
assert_equal 'N', marketing_target
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

0 comments on commit 7575d32

Please sign in to comment.