Hi all this is a function being used to evaluate ML results y_true is the ground truth and y_pred are the predicted values from the machine learning model. This smart F1 function calculates the right threshold for the best F1 score.
I have added comments to the lines where i know what is being done, this is used to find the best threshold for F1 score per class.
What does "fs" stand for ? how is that equal to (precison*recall)/(precision+recall) ?
def f1_smart(y_true, y_pred): # indexes for sort all prediction ascending order args = np.argsort(y_pred) # all true positives tp = y_true.sum() # first thresholds of tp ? inflexion point ? fs = (tp - np.cumsum(y_true[args[:-1]])) / np.arange(y_true.shape + tp - 1, tp, -1) # index of maximum value for fs res_idx = np.argmax(fs) # f1 score f1 = 2 * fs[res_idx] # threshold for f1 score threshold = (y_pred[args[res_idx]] + y_pred[args[res_idx + 1]]) / 2 return f1, threshold