# can you help me figure out this smart f1 function? [closed]

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[0] + 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


## closed as off-topic by mkt, whuber♦Feb 11 at 13:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – mkt, whuber
If this question can be reworded to fit the rules in the help center, please edit the question.

• You demand too much of readers when you present code without any description of its inputs, outputs, or purpose. It looks like there may be a genuine statistical question here, but could you please help us out by providing this contextual information? We have no interest in code review per se. – whuber Feb 11 at 13:10
• hey i did add comments for the code will add some more context. It's not code review i found this useful function which works and is being used by a lot of people on kaggle but nobody is able to explain to me how it works. Thought the community here might be able to figure it out. Yes its an advanced question but hoping you can help. – Janos Neumann Feb 11 at 16:43