Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-003

Entropy from Counts

Problem

Convert raw counts into a probability distribution and return its Shannon entropy in bits:

H = -sum(p_i * log2(p_i)), p_i = c_i / sum(c)

Ignore zero counts. Return 0.0 if the counts sum to zero.

Hint

Normalize the counts first, then apply the entropy formula.

Test cases

case 1
in: [[1,1]]
expected: 1
case 2
in: [[3,1]]
expected: 0.8112781244591328
case 3
in: [[1,2,3,4]]
expected: 1.8464393446710154
case 4
in: [[10]]
expected: 0
case 5
in: [[0,5,5]]
expected: 1
Starter codePython
import math
def entropy_from_counts(counts):
    # Your code here
    pass

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