mirror of
https://github.com/dkam/decisiontree.git
synced 2025-12-28 07:04:53 +00:00
Simplify with #sum
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user