仅作为记录,大佬请跳过。
print(format(TPR,'.4f'))
[对各类分别计算TPR,最后取平均]
用混淆矩阵的形式,用np.array([],[],[],[],[])先列出
# 五分类
import numpy as np
matrix=np.array([[1163,24,5,0,0],[125,413,2,0,0],[2,18,1241,2,0],[0,0,3,1689,0],[326,1405,269,5,1834]])
sum_=matrix.sum()
TPR_list,FPR_list,precision_list,accuracy_list,F1_list=[],[],[],[],[]
def TPF_calculator_twoclass(a,b,c,d):
TPR=float(a/(a+c))
FPR=float(b/(b+d))
precision=float(a/(b+a))
accuracy=float((a+d)/(a+b+c+d))
F1=2*precision*TPR/(precision+TPR)
return TPR,FPR,precision,accuracy,F1
class_num=matrix.shape[0]
for i in range(class_num):
a=matrix[i][i]
b=matrix.sum(axis=0)[i]-a
c=matrix.sum(axis=1)[i]-a
d=sum_-a-b-c
_TPR,_FPR,_precision,_accuracy,_F1=TPF_calculator_twoclass(a,b,c,d)
TPR_list.append(_TPR);FPR_list.append(_FPR);precision_list.append(_precision);accuracy_list.append(_accuracy);F1_list.append(_F1)
print('TPR_list:',TPR_list)
print('FPR_list:',FPR_list)
print('precision_list:',precision_list)
print('accuracy_list:',accuracy_list)
print('F1_list:',F1_list)
print('TPR_average:',np.average(TPR_list))
print('FPR_average:',np.average(FPR_list))
print('precision_average:',np.average(precision_list))
print('accuracy_average:',np.average(accuracy_list))
print('F1_average:',np.average(F1_list))
感谢大佬博主文章——传送门
还可参考: