All problems
Weighted F1 Score
Problem
Compute the support-weighted F1 score over the classes present in y_true.
For each class compute F1 = 2*p*r/(p+r) using 0.0 when a denominator is zero, weight it by support_c / n, and sum the contributions. Empty input returns 0.0.
Hint
Weight each class F1 by its share of the true labels rather than averaging equally.
Test cases
case 1
in: [[0,0,0,1],[0,0,1,1]]
expected: 0.7666666666666667
case 2
in: [[1,1,0,0],[1,1,0,0]]
expected: 1
case 3
in: [[0,1,2,0,1,2],[0,1,2,0,1,2]]
expected: 1
case 4
in: [[],[]]
expected: 0
Starter codePython
def weighted_f1(y_true, y_pred):
# Your code here
pass4 test cases · run your code against them in the editor