Simplify with #sum

This commit is contained in:
Brian Underwood
2017-04-11 14:57:32 -04:00
parent 868ed91672
commit 13aed0b2ae
3 changed files with 5 additions and 4 deletions

BIN
lib/.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -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

View File

@@ -120,12 +120,12 @@ module DecisionTree
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]