All problems
Precision, Recall, F1
Problem
Compute precision, recall, and F1 score for binary classification.
A positive label is 1 by default. Return [precision, recall, f1].
precision = TP / (TP + FP) recall = TP / (TP + FN) F1 = 2 * precision * recall / (precision + recall)
Any undefined value (zero denominator) is 0.0.
Hint
TP = both true and pred are positive. FP = pred positive but true negative. FN = pred negative but true positive.
Test cases
case 1
in: [[1,0,1,1,0],[1,0,0,1,0]]
expected: [1,0.6666666666666666,0.8]
case 2
in: [[1,1,0,0],[1,1,1,0]]
expected: [0.6666666666666666,1,0.8]
case 3
in: [[1,1,1],[0,0,0]]
expected: [0,0,0]
case 4
in: [[0,0,0],[1,1,1]]
expected: [0,0,0]
Starter codePython
def precision_recall_f1(y_true, y_pred, positive=1):
# Returns [precision, recall, f1]
# Your code here
pass4 test cases · run your code against them in the editor