mirror of
https://github.com/dkam/decisiontree.git
synced 2025-12-28 07:04:53 +00:00
Avoid polluting namespace
This commit is contained in:
13
lib/refinements/array_entropy.rb
Normal file
13
lib/refinements/array_entropy.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
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
|
||||
Reference in New Issue
Block a user