I know how to find the optimal threshold for the standard f1 score but do not know how to do so for the weighted f1 score with the sklearn library. Sklearn provides a way to compute the weighted f1-score by passing average = 'weighted'. But it is unclear to me how I can retrieve a list of weighted f1-scores as the probability threshold of my true class prediction varies.

When dealing with the standard f1-score computation, the process is straightforward as I do not need to use the sklearn function to actually compute the f1-score.

For example I do the following:

precision, recall, thresholds = precision_recall_curve(y_true, y_score)
f1_scores = 2*recall*precision/(recall+precision)
print('Best threshold: ', thresholds[np.argmax(f1_scores)])
print('Best F1-Score: ', np.max(f1_scores))

I suppose the root of my problem is that I do not understand exactly how the "weighted f1-score" is calculated otherwise I could compute it manually as I did with the standard f1-score.

How can I find the optimal weighted f1 score?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.