Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-333

Conditional Entropy from Counts

Problem

Compute the conditional entropy H(Y|X) in bits from a 2D table of counts, where rows index the values of X and columns the values of Y. Each row contributes its probability mass times the entropy of its conditional distribution. Return 0.0 for an empty or all-zero table.

Hint

Use H(Y|X) = sum_x p(x) H(Y | X = x), skipping empty rows.

Test cases

case 1
in: [[[1,1],[1,1]]]
expected: 1
case 2
in: [[[2,0],[0,2]]]
expected: 0
case 3
in: [[[1,2],[3,4]]]
expected: 0.965148445440323
case 4
in: [[[0,0],[0,0]]]
expected: 0
case 5
in: [[[3,1],[1,3]]]
expected: 0.8112781244591329
Starter codePython
import math
def conditional_entropy_counts(counts):
    # Your code here
    pass

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