Skip to content

Instantly share code, notes, and snippets.

@shoyan shoyan/entropy.rb
Created Jun 24, 2016

Embed
What would you like to do?
雑なエントロピーの計算
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
You can’t perform that action at this time.