How do I compute the approximate entropy of a bit string?

前端 未结 7 2083
梦如初夏
梦如初夏 2020-12-07 20:46

Is there a standard way to do this?

Googling -- \"approximate entropy\" bits -- uncovers multiple academic papers but I\'d like to just find a chunk of pseudocode de

7条回答
  •  臣服心动
    2020-12-07 21:00

    Using Shannon entropy of a word with this formula : http://imgur.com/a/DpcIH

    Here's a O(n) algorithm that calculates it :

    import math
    from collections import Counter
    
    
    def entropy(s):
        l = float(len(s))
        return -sum(map(lambda a: (a/l)*math.log2(a/l), Counter(s).values()))
    

提交回复
热议问题