[英]arrays TP, TN, FP and FN in Python
我的预测结果是这样的
测试数组
[1,0,0,0,1,0,1,...,1,0,1,1],
[1,0,1,0,0,1,0,...,0,1,1,1],
[0,1,1,1,1,1,0,...,0,1,1,1],
.
.
.
[1,1,0,1,1,0,1,...,0,1,1,1],
预测数组
[1,0,0,0,0,1,1,...,1,0,1,1],
[1,0,1,1,1,1,0,...,1,0,0,1],
[0,1,0,1,0,0,0,...,1,1,1,1],
.
.
.
[1,1,0,1,1,0,1,...,0,1,1,1],
这是我拥有的数组的大小
TestArray.shape
Out[159]: (200, 24)
PredictionArray.shape
Out[159]: (200, 24)
我想为这些阵列获得 TP、TN、FP 和 FN
我试过这个代码
cm=confusion_matrix(TestArray.argmax(axis=1), PredictionArray.argmax(axis=1))
TN = cm[0][0]
FN = cm[1][0]
TP = cm[1][1]
FP = cm[0][1]
print(TN,FN,TP,FP)
但我得到的结果
TN = cm[0][0]
FN = cm[1][0]
TP = cm[1][1]
FP = cm[0][1]
print(TN,FN,TP,FP)
125 5 0 1
我检查了厘米的形状
cm.shape
Out[168]: (17, 17)
125 + 5 + 0 + 1 = 131 这不等于我拥有的列数 200
我期望有 200,因为阵列中的每个单元格假设是 TF、TN、FP、TP,所以总数应该是 200
如何解决?
这是问题的一个例子
import numpy as np
from sklearn.metrics import confusion_matrix
TestArray = np.array(
[
[1,0,0,1,0,1,1,0,1,0,1,1,0,0,1,1,1,0,0,1],
[0,1,1,0,1,0,0,1,0,0,0,1,0,1,0,1,1,0,1,1],
[1,0,1,1,1,1,0,0,1,1,1,1,0,0,1,0,0,0,0,0],
[0,1,1,1,0,0,0,0,0,1,0,0,1,0,0,1,0,1,1,1],
[0,0,0,0,1,1,0,1,1,0,0,1,0,1,1,0,1,1,1,1],
[1,0,0,1,1,1,0,1,1,0,1,0,0,1,1,0,0,1,0,0],
[1,1,1,0,0,1,0,0,1,1,0,1,0,1,1,1,1,1,0,1],
[0,0,0,1,0,0,1,0,1,0,1,0,0,0,0,1,0,0,1,1],
[1,0,1,0,0,0,0,1,0,1,0,1,0,0,0,0,1,0,1,0],
[1,1,0,1,1,1,1,0,1,0,1,0,1,1,1,1,0,1,0,0]
])
TestArray.shape
PredictionArray = np.array(
[
[0,0,0,1,1,1,1,0,0,0,1,0,0,0,1,0,1,0,1,1],
[0,1,0,0,1,0,1,1,0,0,0,1,1,0,0,1,1,0,0,1],
[1,1,0,1,1,1,0,0,0,0,0,1,0,0,1,0,0,1,0,0],
[0,1,0,1,0,0,1,0,0,1,0,1,1,0,0,1,0,0,1,1],
[0,0,1,0,0,1,0,1,1,1,0,1,1,1,0,0,1,1,0,1],
[1,0,0,1,0,1,1,1,1,0,0,1,0,1,1,1,0,1,1,0],
[1,1,0,0,1,1,0,0,0,1,0,1,0,0,1,1,0,1,0,1],
[0,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1],
[1,0,1,1,0,0,0,1,0,1,0,1,1,1,1,0,0,0,1,0],
[1,1,0,1,1,1,1,1,1,0,1,0,0,0,0,1,1,1,0,0]
])
PredictionArray.shape
cm=confusion_matrix(TestArray.argmax(axis=1), PredictionArray.argmax(axis=1))
TN = cm[0][0]
FN = cm[1][0]
TP = cm[1][1]
FP = cm[0][1]
print(TN,FN,TP,FP)
输出是
5 0 2 0
= 5+0+2+0 = 7 !!
数组中有 20 列和 10 行
但厘米给总共 7 !
使用np.argmax
,您输入的矩阵sklearn.metrics.confusion_matrix
不再是二进制的,因为np.argmax
返回第一个出现的最大值的索引。 在这种情况下,沿axis=1
。
当您的预测不是二进制时,您不会得到好的真阳性/命中、真阴性/正确拒绝等。
您应该会发现sum(sum(cm))
确实等于 200。
如果数组的每个索引代表一个单独的预测,即您试图获得 TP/TN/FP/FN 总共 200 ( 10 * 20
) 个预测,每个预测的结果为0
或1
,那么您可以在将数组解析为confusion_matrix
矩阵之前,通过展平数组来获得 TP/TN/FP/FN。 也就是说,您可以将TestArray
和PreditionArry
重塑为(200,)
,例如:
cm = confusion_matrix(TestArray.reshape(-1), PredictionArray.reshape(-1))
TN = cm[0][0]
FN = cm[1][0]
TP = cm[1][1]
FP = cm[0][1]
print(TN, FN, TP, FP, '=', TN + FN + TP + FP)
哪个返回
74 28 73 25 = 200
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.