gpt4 book ai didi

python - 如何从keras指标中获取TPR和TNR

转载 作者:行者123 更新时间:2023-11-30 09:44:19 25 4
gpt4 key购买 nike

我想获取 model.compile() 语句中的真阳性率 (TPR) 和真阴性率 (TNR) 作为评估指标之一。

我尝试使用以下代码:

model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ["accuracy", "tpr", "tnr"])

但是我收到一条错误消息:

Unknown metric function: tpr

我相信它们都是 keras 中的已知指标,所以我不明白这个错误。请帮忙

最佳答案

从相关的Keras docs可以清楚地看出、tprtnr 不是 native Keras 指标的一部分;有相关Github thread ,但问题仍然悬而未决。

但是对于您似乎正在处理的二进制情况,从 scikit-learn 获取所需的数量很简单(您需要将模型结果转换为二进制标签,即不是概率);改编 docs 中的示例:

from sklearn.metrics import confusion_matrix
y_true = [0, 1, 0, 1]
y_pred = [1, 1, 1, 0]
cm = confusion_matrix(y_true, y_pred) # careful with the order of arguments!
tn, fp, fn, tp = cm.ravel()
(tn, fp, fn, tp)
# (0, 2, 1, 1)

获得这些量后,现在可以直接计算 TPR 和 TNR(请参阅 Wikipedia 中的定义):

TPR = tp/(tp+fn)
TPR
# 0.5

TNR = tn/(tn+fp)
TNR
# 0.0

多类情况有点复杂 - 请参阅我的回答 How to get precision, recall and f-measure from confusion matrix in Python .

关于python - 如何从keras指标中获取TPR和TNR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com