简体   繁体   中英

How to find TN,TP,FN,FP from matrix in python?

I have code like this.

import numpy as np
confusion_matrix = [[6,0,0,0,0], 
                    [0,62,1,1,0], 
                    [0,0,30,0,0], 
                    [0,1,0,41,0], 
                    [0,0,0,0,2]]
confusion_matrix=np.matrix(confusion_matrix)
FP = confusion_matrix.sum(axis=0) - np.diag(confusion_matrix)  
FN = confusion_matrix.sum(axis=1) - np.diag(confusion_matrix)
TP = np.diag(confusion_matrix)
TN = confusion_matrix.values.sum() - (FP + FN + TP)

# Sensitivity, hit rate, recall, or true positive rate
TPR = TP/(TP+FN)
# Specificity or true negative rate
TNR = TN/(TN+FP) 
# Precision or positive predictive value
PPV = TP/(TP+FP)
# Negative predictive value
NPV = TN/(TN+FN)
# Fall out or false positive rate
FPR = FP/(FP+TN)
# False negative rate
FNR = FN/(TP+FN)
# False discovery rate
FDR = FP/(TP+FP)

# Overall accuracy
ACC = (TP+TN)/(TP+FP+FN+TN)

But it gives an error like this.

AttributeError: 'matrix' object has no attribute 'values' How to find all TP,FP,TN,FN for given matrix.

将此confusion_matrix.values.sum() - (FP + FN + TP)更改为

confusion_matrix.sum() - (FP + FN + TP)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM