Skip to content
DeepForgeby svx

Problem

Compute the Shannon entropy (base 2) of a list of class labels:

H = -sum(p_i * log2(p_i))

where p_i is the proportion of label i. Empty input returns 0.0.

Hint

Use math.log2 for base-2 entropy.

Test cases

case 1
in: [[0,0,1,1]]
expected: 1
case 2
in: [[0,0,0,0]]
expected: 0
case 3
in: [[0,1,2,3]]
expected: 2
case 4
in: [[]]
expected: 0
Starter codePython
import math
def entropy(labels):
    # Your code here
    pass

4 test cases · run your code against them in the editor