inf*_*nge 5 python statistics classification scikit-learn precision-recall
对于一个简单的二元分类问题,我想找出什么阈值设置使 f1 分数最大化,即精度和召回率的调和平均值。scikit learn 中是否有任何内置功能可以做到这一点?现在,我只是打电话
precision, recall, thresholds = precision_recall_curve(y_test, y_test_predicted_probas)
Run Code Online (Sandbox Code Playgroud)
然后,我可以使用数组三元组中每个索引处的信息计算 f1 分数:
curr_f1 = compute_f1(precision[index], recall[index])
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点,或者这是图书馆打算如何使用?谢谢。
小智 8
有时precision_recall_curve会选择一些对于数据来说太高的阈值,因此最终会得到precision和recall均为零的点。nan计算 F1 分数时这可能会导致s。为了确保正确的输出,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)]
Run Code Online (Sandbox Code Playgroud)
小智 7
计算精度、召回率和阈值分数后,您将获得 NumPy 数组。
只需使用 NumPy 函数来找到最大化 F1-Score 的阈值:
f1_scores = 2*recall*precision/(recall+precision)
print('Best threshold: ', thresholds[np.argmax(f1_scores)])
print('Best F1-Score: ', np.max(f1_scores))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2658 次 |
| 最近记录: |