获取 Python 中数组的真阳性、TN、FP 和 FN

get True Positives, TN, FP and FN for arrays in Python

我的数据集结果是这样的

yval
Out[59]: 
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [1, 0, 0, ..., 0, 0, 0]])

预测结果如下所示

y_pred
Out[60]: 
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]])

我要查找 TP、TN、FP 和 FP

我试过了

cm=confusion_matrix(yval, y_pred)

出现这个错误

ValueError: multilabel-indicator is not supported

试过这个

cm=confusion_matrix(yval.argmax(axis=1), y_pred.argmax(axis=1))
TN = cm[0][0]
FN = cm[1][0]
TP = cm[1][1]
FP = cm[0][1]

为所有值赋零 TN=0, FN=0, TP=0 and FP=0

如何获得预测数组的这些值?

您可以使用from sklearn.metrics import multilabel_confusion_matrix导入多标签混淆矩阵。 之后的演练是:

cm = multilabel_confusion_matrix(yval, y_pred)