From b310a7f440fd715863faa96fb192708fd03ff394 Mon Sep 17 00:00:00 2001 From: Sergey Dolganov Date: Tue, 30 Aug 2016 13:25:08 +0500 Subject: [PATCH] Fix infinite recursion (solution by @nicomahler) Closes https://github.com/igrigorik/decisiontree/issues/19 --- lib/decisiontree/id3_tree.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/decisiontree/id3_tree.rb b/lib/decisiontree/id3_tree.rb index b0702c8..19c6636 100755 --- a/lib/decisiontree/id3_tree.rb +++ b/lib/decisiontree/id3_tree.rb @@ -58,6 +58,7 @@ def id3_train(data, attributes, default, _used={}) # 2. Pick best attribute # 3. If attributes all score the same, then pick a random one to avoid infinite recursion. performance = attributes.collect { |attribute| fitness_for(attribute).call(data, attributes, attribute) } + return data.first.last if performance.all? { |a, b| a.to_f <= 0 } max = performance.max { |a,b| a[0] <=> b[0] } min = performance.min { |a,b| a[0] <=> b[0] } max = performance.sample if max[0] == min[0]