Skip to content
DeepForgeby svx
All problems
HardML Fundamentalsml-151

Normalized Mutual Information

Problem

Compute the normalized mutual information between two label lists in bits: NMI = 2 * MI / (H(a) + H(b)), where MI uses joint and marginal probabilities and H is Shannon entropy base 2.

Return 0.0 if the entropies sum to 0 or the input is empty.

Hint

Normalizing by the average entropy keeps NMI between 0 and 1.

Test cases

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

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