Skip to content

Commit

Permalink
Style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius committed Nov 20, 2015
1 parent 8a8cc1e commit 6adbebb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 28 deletions.
1 change: 1 addition & 0 deletions lib/decisiontree.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require File.dirname(__FILE__) + '/decisiontree/id3_tree.rb'
require 'core_extension/array.rb'
31 changes: 3 additions & 28 deletions lib/decisiontree/id3_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,6 @@ def self.load_from_file(filename)
end
end

class Array
def classification
collect(&:last)
end

# calculate information entropy
def entropy
return 0 if empty?

info = {}
total = 0
each do |i|
info[i] = !info[i] ? 1 : (info[i] + 1)
total += 1
end

result = 0
info.each do |_symbol, count|
if count > 0
result += -count.to_f / total * Math.log(count.to_f / total) / Math.log(2.0)
end
end
result
end
end

module DecisionTree
Node = Struct.new(:attribute, :threshold, :gain)

Expand Down Expand Up @@ -105,9 +79,10 @@ def id3_train(data, attributes, default, _used={})
fitness = fitness_for(best.attribute)
case type(best.attribute)
when :continuous
data.partition do |d|
partitioned_data = data.partition do |d|
d[attributes.index(best.attribute)] >= best.threshold
end.each_with_index do |examples, i|
end
partitioned_data.each_with_index do |examples, i|
tree[best][String.new(l[i])] = id3_train(examples, attributes, (data.classification.mode rescue 0), &fitness)
end
when :discrete
Expand Down

0 comments on commit 6adbebb

Please sign in to comment.