使用 for 循环创建阈值评估

时间:2021-04-29 10:12:09

标签: python for-loop

我在这里尝试使用 for 循环来自动运行所有阈值! 如何使用阈值在此处创建 for 循环?

y_pred_binary = []
thresholds = [0.4, 0.45, 0.50, 0.55, 0.60, 0.65]

for pred in y_pred:
    if pred > 0.5:
        y_pred_binary.append(1)
    else:
        y_pred_binary.append(0)

通过使用这个命令:

print(classification_report(target, y_pred_binary, labels=[0, 1]))
print(evalute(target, y_pred, y_pred_binary))

我想打印出每个阈值的所有结果! 例如:

#classification report 

          precision    recall  f1-score   support

           0       0.76      0.20      0.31      2162
           1       0.54      0.94      0.68      2162

    accuracy                           0.57      4324
   macro avg       0.65      0.57      0.50      4324
weighted avg       0.65      0.57      0.50      4324


#result of evaluate function I made 

Accuracy: 0.567
f1: 0.684
recall: 0.937
precision: 0.538
rocauc: 0.769
[[ 425 1737]
 [ 136 2026]]

1 个答案:

答案 0 :(得分:0)

您正在寻找以下实现吗?请详细说明要求。

y_pred_binary = []
thresholds = [0.4, 0.45, 0.50, 0.55, 0.60, 0.65]

for thresh in thresholds :
    for pred in y_pred:
        if pred > thresh :
            y_pred_binary.append(1)
        else:
            y_pred_binary.append(0)

编辑:


thresholds = [0.4, 0.45, 0.50, 0.55, 0.60, 0.65]
y_pred_binary = [[0 for i in range(len(y_pred))] for j in range(len(thresholds) )]

for a  in range(len(thresholds)) :
    for b in range(len(y_pred)):
        if y_pred[b]> thresholds[a]:
            y_pred_binary[a][b] =1
        

y_pred_binary 现在是所有阈值的二维数组

相关问题