Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rescue error that is raised when ID3 prediction fails on a row of data

  • Loading branch information...
commit c95f000f9dca3f76e56696d9615cb93af5f9d546 1 parent c06f3ab
@RobinLassonde RobinLassonde authored
Showing with 18 additions and 2 deletions.
  1. +6 −2 lib/ai4r/classifiers/id3.rb
  2. +12 −0 test/classifiers/id3_test.rb
View
8 lib/ai4r/classifiers/id3.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 []
View
12 test/classifiers/id3_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.