def shannon_entropy ( p ) : return - np . sum ( np . where ( p != 0 , p * np . log2 ( p ) , 0 ) )
python compute shannon entropy
Calculates shannon entropy in bits .
def shannon_entropy(p):
    """Calculates shannon entropy in bits.

    Parameters
    ----------
    p : np.array
        array of probabilities

    Returns
    -------
    shannon entropy in bits
    """
    return -np.sum(np.where(p!=0, p * np.log2(p), 0))