Ad

Shannon's Entropy is the "amount of information" in a variable. It translates to the amount of storage (e.g. number of bits) required to store the variable, which can intuitively be understood to correspond to the amount of information in that variable.

Write a function that returns the shannon entropy for a given string.

import math

def shannon_entropy(s):
    symbols = list(s)
    unique_symbols = set(s)
    M = float(len(s))
    entropy_list = []
    for x in unique_symbols:
        n_i = symbols.count(x)
        P_i = n_i / M
        entropy_i = P_i * (math.log(P_i, 2))
        entropy_list.append(entropy_i)
    sh_entropy = -(sum(entropy_list))
    return sh_entropy