Shannon Entropy coding

Question:

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

Asked By: su sahin

||

Answers:

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.

Answered By: John Coleman
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.