Sha256: 22e221d8613304288eaf5fe0c55dafadf4635e5f4b8d4c589ea182a9d696bff2
Contents?: true
Size: 514 Bytes
Versions: 1
Compression:
Stored size: 514 Bytes
Contents
require "shannon/version" module Shannon # Calculate Shannon entropy for a string. # # @example # Shannon::entropy("abcde", 2).round(4) #=> 2.3219 # # @param str [String] string to calculate entropy of # @param base [Fixnum] base of the log for calculation def self.entropy str, base=2 len = str.length.to_f str.each_char. group_by(&:itself). values. map { |ary| ary.length / len }. reduce(0) { |entropy, freq| entropy - freq * Math.log(freq, base) } end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
shannon-0.1.1 | lib/shannon.rb |