从比较中生成数组

Generate array from comparison

我希望 TPP 是一个数组,其中包含每个阈值的 TPP 值。 印刷品应该是这样的:TPP 是:n1, n2...

threshold=[0, 6, 15]
df=[3,13,19,21]
y_true=[0,0,1,0]
y_pred=np.where(df<threshold,1,0)
cm=confusion_matrix(y_true,y_pred)
TP=cm[0,0]
FN=cm[0,1]
TPP=TP/(TP+FN)
print('TPP is:',TPP)

在我看来,这段代码实现了您的目标:

from sklearn.metrics import confusion_matrix
import numpy as np

threshold = [0, 6, 15, 20]
df        = [3,13,19,21]
y_true    = [0,0,1,0]

print('TPP is:', end=' ')
for idx, i in enumerate(threshold):
    y_pred = np.where(np.array(df) < i,1,0)
    are_ones = y_true == np.ones(len(y_true))
    predicted_as_zero = y_pred == np.zeros(len(y_true))
    TP = sum( are_ones   & np.array(y_pred) )
    FN = sum( are_ones & predicted_as_zero )
    if idx+1 == len(threshold):
        print(TP/(TP+FN), end='')
    else:
        print(TP/(TP+FN), end=', ')