All problems
Threshold Sweep Best F1
Problem
Find the probability threshold maximizing F1. Sweep unique probabilities from high to low, predicting positive when p >= t, and keep the best F1 (ties keep the highest threshold).
Return [threshold, f1]; no positive labels or empty input returns [0.0, 0.0].
Hint
F1 balances precision and recall, so the best threshold need not be 0.5.
Test cases
case 1
in: [[0,0,1,1],[0.1,0.4,0.35,0.8]]
expected: [0.35,0.8]
case 2
in: [[1,1,1],[0.2,0.5,0.8]]
expected: [0.2,1]
case 3
in: [[0,0],[0.1,0.9]]
expected: [0,0]
case 4
in: [[],[]]
expected: [0,0]
Starter codePython
def best_f1_threshold(y_true, probs):
# Returns [threshold, f1]
# Your code here
pass4 test cases · run your code against them in the editor