Method: Array#entropy
- Defined in:
- lib/core/facets/array/entropy.rb
#entropy ⇒ Object
Shannon’s entropy for an array - returns the average bits per symbol required to encode the array. Lower values mean less “entropy” - i.e. less unique information in the array.
e = %w{ a b c d e e e }.entropy
("%.3f" % e) #=> "2.128"
CREDIT: Derek
16 17 18 19 20 21 22 23 24 |
# File 'lib/core/facets/array/entropy.rb', line 16 def entropy arr = self probHash = arr.probability # -- h is the Shannon entropy of the array h = -1.to_f * probHash.keys.inject(0.to_f) do |sum, i| sum + (probHash[i] * (Math.log(probHash[i])/Math.log(2.to_f))) end h end |