Home > Software engineering >  Shannon Entropy coding
Shannon Entropy coding

Time:11-16

I am trying to write a Shannon entropy code and test it. Although testing with "aaaaa" should give me 0.0. it gives me -0.0 is there anyway to fix that?

[CODE]

1

CodePudding user response:

Since -0.0 == 0.0 is true, what you have is mathematically correct. If you find the output unaesthetic, the fix is simple. Don't multiply by -1. Take the absolute value instead:

from collections import Counter
import math

def entropy(string):
    counts = Counter(string)
    rel_freq = ((i/len(string)) for i in counts.values())
    return abs(sum(f*math.log2(f) for f in rel_freq))

Then entropy('aaaaa') evaluates to 0.0.

  • Related