Skip to content

Commit

Permalink
Simplify with #sum
Browse files Browse the repository at this point in the history
  • Loading branch information
cheerfulstoic committed Apr 11, 2017
1 parent 868ed91 commit 13aed0b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
Binary file added lib/.DS_Store
Binary file not shown.
5 changes: 3 additions & 2 deletions lib/core_extensions/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ class Array
def entropy
each_with_object(Hash.new(0)) do |i, result|
result[i] += 1
end.values.inject(0) do |sum, count|
end.values.sum do |count|
percentage = count.to_f / length
sum + -percentage * Math.log2(percentage)

-percentage * Math.log2(percentage)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/decisiontree/id3_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ def id3_discrete(data, attributes, attribute)
index = attributes.index(attribute)

values = data.map { |row| row[index] }.uniq
remainder = values.sort.inject(0) do |sum, val|
remainder = values.sort.sum do |val|
classification = data.each_with_object([]) do |row, result|
result << row.last if row[index] == val
end

sum + ((classification.size.to_f / data.size) * classification.entropy)
((classification.size.to_f / data.size) * classification.entropy)
end

[data.classification.entropy - remainder, index]
Expand Down

0 comments on commit 13aed0b

Please sign in to comment.