igrigorik / decisiontree

ID3-based implementation of the ML Decision Tree algorithm
1.44k stars 130 forks source link

Fix infinite recursion. #8

Closed rustyio closed 11 years ago

rustyio commented 11 years ago
require 'decisiontree'

labels = [:a, :b, :c]
data = [
  ["a1", "b0", "c0", "RED"],
  ["a1", "b1", "c1", "RED"],
  ["a1", "b1", "c0", "BLUE"],
  ["a1", "b0", "c1", "BLUE"]
]

tree = DecisionTree::ID3Tree.new(labels, data, "RED", :discrete)
tree.train

The preceding sample causes infinite recursion.

When all attributes result in the same fitness score, the code continuously chooses to partition on the first attribute. As you can see in the example above, this does nothing to partition the data.

While it may not be the provably best approach, this pull request solves the problem by checking if all attributes result in the same fitness score, and if so, choosing a best attribute randomly.

igrigorik commented 11 years ago

Nice, thanks!