Skip to content

Instantly share code, notes, and snippets.

@shoyan
Created June 24, 2016 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoyan/bcc4a8d3d817c351ffc97595a7dd87d8 to your computer and use it in GitHub Desktop.
Save shoyan/bcc4a8d3d817c351ffc97595a7dd87d8 to your computer and use it in GitHub Desktop.
雑なエントロピーの計算
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