从比较生成数组

Posted

技术标签:

【中文标题】从比较生成数组【英文标题】:Generate array from comparison 【发布时间】:2022-01-13 13:21:16 【问题描述】:

我希望 TPP 是一个数组,其中包含每个阈值的 TPP 值。 打印应该是这样的:TPP is: 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)

【问题讨论】:

如果您使用 Python,为什么要使用 s 标签? "threshold" 可能应该是一个常数,如果您正在检测“df”的值(在 python 中是一个重载名称) 你的意思是“真阳性率”吗?如果是这样,它应该是 [0.0, 1.0] 范围内的单个数字 我不知道为什么会有's'标签。可能是注意力不集中。 【参考方案1】:

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

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=', ')

【讨论】:

谢谢,但是有没有办法将 TP 和 FN 存储为数组,而不是一次打印一个?

以上是关于从比较生成数组的主要内容,如果未能解决你的问题,请参考以下文章

算法实战生成窗口最大值数组

两个STRING数组进行比较

比较相同值的多个数组

将选定日期与表列或 MySQL 查询生成的日期数组进行比较

如何从数组值生成多维数组

从php中的数组生成多维数组?