All problems
Inverse-Frequency Class Weights
Problem
Compute inverse-frequency class weights. For each label c, weight = n / (k * count_c), where n is the number of samples and k is the number of distinct labels.
Return a dict mapping str(label) to its weight. Empty input returns {}.
Hint
Rarer classes get larger weights; keys are stringified labels.
Test cases
case 1
in: [["a","a","a","b","b","c"]]
expected: {"a":0.6666666666666666,"b":1,"c":2}
case 2
in: [[0,0,1,1,1,1]]
expected: {"0":1.5,"1":0.75}
case 3
in: [["x"]]
expected: {"x":1}
case 4
in: [[]]
expected: {}
Starter codePython
def inverse_frequency_weights(labels):
# Your code here
pass4 test cases · run your code against them in the editor