mirror of
https://github.com/dkam/decisiontree.git
synced 2025-12-28 15:14:52 +00:00
14 lines
284 B
Ruby
14 lines
284 B
Ruby
module ArrayEntropy
|
|
refine Array do
|
|
def entropy
|
|
each_with_object(Hash.new(0)) do |i, result|
|
|
result[i] += 1
|
|
end.values.inject(0, :+) do |count|
|
|
percentage = count.to_f / length
|
|
|
|
-percentage * Math.log2(percentage)
|
|
end
|
|
end
|
|
end
|
|
end
|