This question already has answers here:
How to get precision, recall and f-measure from confusion matrix in Python [duplicate]
(3个答案)
calculate precision and recall in a confusion matrix
(4个答案)
在5个月前关闭。
您如何计算多类别分类问题的正确率和错误率?说,
混淆矩阵是由
@seralouk的答案后进行编辑。在此,将
对于我们有很多类的一般情况,这些指标在下图中以图形方式表示:
(3个答案)
calculate precision and recall in a confusion matrix
(4个答案)
在5个月前关闭。
您如何计算多类别分类问题的正确率和错误率?说,
y_true = [1, -1, 0, 0, 1, -1, 1, 0, -1, 0, 1, -1, 1, 0, 0, -1, 0]
y_prediction = [-1, -1, 1, 0, 0, 0, 0, -1, 1, -1, 1, 1, 0, 0, 1, 1, -1]
混淆矩阵是由
metrics.confusion_matrix(y_true, y_prediction)
计算的,但这只是解决了问题。@seralouk的答案后进行编辑。在此,将
-1
类视为底片,而0
和1
是底片的变体。 最佳答案
使用数据,您可以一次获取所有类的所有指标:
import numpy as np
from sklearn.metrics import confusion_matrix
y_true = [1, -1, 0, 0, 1, -1, 1, 0, -1, 0, 1, -1, 1, 0, 0, -1, 0]
y_prediction = [-1, -1, 1, 0, 0, 0, 0, -1, 1, -1, 1, 1, 0, 0, 1, 1, -1]
cnf_matrix = confusion_matrix(y_true, y_prediction)
print(cnf_matrix)
#[[1 1 3]
# [3 2 2]
# [1 3 1]]
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)
# 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)
对于我们有很多类的一般情况,这些指标在下图中以图形方式表示:
10-08 04:15