对于一个简单的二进制分类问题,我想找出什么阈值设置可以最大化f1分数,这是精度和召回率的调和平均值。在scikit learn中有没有内置的工具可以做到这一点?现在,我只是简单地打电话给
precision, recall, thresholds = precision_recall_curve(y_test, y_test_predicted_probas)然后,我可以使用三元组数组中每个索引处的信息来计算f1分数:
curr_f1 = compute_f1(precision[index], recall[index])有没有更好的方法来做这件事,或者这就是这个库的用法?谢谢。
发布于 2019-10-10 19:24:57
在计算精确度、召回率和阈值分数后,您将获得NumPy数组。
只需使用NumPy函数即可找到使F1得分最大化的阈值:
f1_scores = 2*recall*precision/(recall+precision)
print('Best threshold: ', thresholds[np.argmax(f1_scores)])
print('Best F1-Score: ', np.max(f1_scores))发布于 2021-03-09 22:41:11
有时,precision_recall_curve会选取一些对数据来说太高的阈值,因此最终得到precision和recall都为零的点。在计算F1分数时,这可能会导致nan。为了确保正确的输出,使用np.divide仅在分母为非零时进行除法:
precision, recall, thresholds = precision_recall_curve(y_test, y_test_predicted_probas)
numerator = 2 * recall * precision
denom = recall + precision
f1_scores = np.divide(numerator, denom, out=np.zeros_like(denom), where=(denom!=0))
max_f1 = np.max(f1_scores)
max_f1_thresh = thresholds[np.argmax(f1_scores)]https://stackoverflow.com/questions/57060907
复制相似问题