Created
June 24, 2016 09:19
-
-
Save shoyan/bcc4a8d3d817c351ffc97595a7dd87d8 to your computer and use it in GitHub Desktop.
雑なエントロピーの計算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
str="abccddeeeeffffgggggggghhhhhhhh" | |
def entropy(str) | |
count = str.size | |
list={} | |
list.default=0 | |
str.split("").each {|c| list[c]+=1} | |
sum = 0 | |
list.each do |key, val| | |
i = (val.to_f / count.to_f) | |
sum += -(i) * Math.log(i, 2) | |
end | |
sum | |
end | |
e = entropy(str) | |
p e, str.size * e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment