Files
decisiontree/lib/core_extensions/array.rb
Brian Underwood 5be22ab1f6 Update array.rb
2017-05-09 16:13:18 -04:00

21 lines
346 B
Ruby

class Array
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
module ArrayClassification
refine Array do
def classification
collect(&:last)
end
end
end