【问题标题】: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'标签。可能是注意力不集中。

标签: python arrays loops 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 存储为数组,而不是一次打印一个?
猜你喜欢
  • 1970-01-01
  • 2018-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
相关资源
最近更新 更多