【发布时间】:2019-09-29 00:08:52
【问题描述】:
我正在研究 python 中的多类分类(4 类)。 为了分别获取每个类的结果,我使用了以下代码:
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
cnf_matrix = cm
FP = cnf_matrix.sum(axis=0) - np.diag(cnf_matrix)
FN = cnf_matrix.sum(axis=1) - np.diag(cnf_matrix)
TP = np.diag(cnf_matrix)
TN = cnf_matrix.sum() - (FP + FN + TP)
FP = FP.astype(float)
FN = FN.astype(float)
TP = TP.astype(float)
TN = TN.astype(float)
# Sensitivity, hit rate, recall, or true positive rate
TPR = TP/(TP+FN)
print('TPR : ',TPR)
# Specificity or true negative rate
TNR = TN/(TN+FP)
print('TNR : ',TNR)
# Precision or positive predictive value
PPV = TP/(TP+FP)
print('PPV : ',PPV)
# Fall out or false positive rate
FPR = FP/(FP+TN)
print('FPR : ',FPR)
# False negative rate
FNR = FN/(TP+FN)
print('FNR : ',FNR)
# Overall accuracy
ACC = (TP+TN)/(TP+FP+FN+TN)
print('ACC : ',ACC)
我得到了以下结果:
TPR : [0.98398792 0.99999366 0.99905393 0.99999548]
TNR : [0.99999211 0.99997989 1. 0.99773928]
PPV : [0.99988488 0.99996832 1. 0.99810887]
FPR : [7.89469529e-06 2.01061605e-05 0.00000000e+00 2.26072224e-03]
FNR : [1.60120846e-02 6.33705530e-06 9.46073794e-04 4.52196090e-06]
ACC : [0.99894952 0.99998524 0.99999754 0.99896674]
现在,我想计算每个指标的平均值?!我应该将这四个值相加,然后将结果除以 4 吗?例如,对于准确度 (ACC):(0.99894952 + 0.99998524 + 0.99999754 + 0.99896674)/4 ?!! 或者我应该怎么做? 请帮忙。
【问题讨论】:
-
是的,平均计算方式就是你说的,这有什么问题?
-
感谢您的回答,先生,我用这种方法计算了Acc,结果是:0.99947476。之后我使用了:“from sklearn.metrics import accuracy_score”“accuracy_score(y_test,y_pred)”,结果不同了:0.99894952 为什么?第二种方法应该直接给我平均结果,但它与我之前提到的方式不同。
-
你好,我还没有使用
from sklearn.metrics import accuracy_score,我也不知道为什么,但是0.99947476的答案是真的,这样算
标签: python machine-learning multiclass-classification