Machine learning 多类案例的混淆矩阵,所有评估指标的估计
我正在用ML进行一些自我学习。我正在尝试训练一个K-NN模型,我的模型给出了以下7X7混淆矩阵。我已经手动计算了两个类(a和B)的准确度但我不确定我的方法是否正确。如果我的结果得到确认,我想找其他人,这样我就可以写一个程序来计算其他5门课Machine learning 多类案例的混淆矩阵,所有评估指标的估计,machine-learning,scikit-learn,confusion-matrix,Machine Learning,Scikit Learn,Confusion Matrix,我正在用ML进行一些自我学习。我正在尝试训练一个K-NN模型,我的模型给出了以下7X7混淆矩阵。我已经手动计算了两个类(a和B)的准确度但我不确定我的方法是否正确。如果我的结果得到确认,我想找其他人,这样我就可以写一个程序来计算其他5门课 Confusion Matrx : A B C D E F G = 7 classes [[ 238 1 2 0 41 11 0] [ 0 25 0 0
Confusion Matrx :
A B C D E F G = 7 classes
[[ 238 1 2 0 41 11 0]
[ 0 25 0 0 3 1 0]
[ 21 1 32 0 17 4 0]
[ 0 0 0 7 9 3 0]
[ 7 0 0 0 3633 8 0]
[ 44 0 4 1 397 256 1]
[ 4 0 0 0 7 2 3]]
Class-A
tp = 238
fp = 76 (Rest of the 'a' column e.g 21+0+7+44+4 = 76)
tn = 4414
[25 0 0 3 1 0]
[1 32 0 17 4 0]
[0 0 7 9 3 0]
[0 0 0 3633 8 0]
[0 4 1 397 256 1]
[0 0 0 7 2 3]
Sum of all elements will be true negative = 4414
fn = 55 (Rest of the 'a' rows element e.g 1+2+0+41+11+0 =55 )
So class-A confusion matrix will something look like below
tp fp | 238 76
|
fn tn | 55 4414
Class A Accuracy = tp+tn/tp+tn+fp+fn = 4652/4768 = 0.97
Class B
tp = 25
fp = 2
tn = 4752
[ 238 2 0 41 11 0]
[ 21 32 0 17 4 0]
[ 0 0 7 9 3 0]
[ 7 0 0 3633 8 0]
[ 44 4 1 397 256 1]
[ 4 0 0 7 2 3]
fn = 4
So class-B confusion matrix will something look like below
tp fp | 2 76
|
fn tn | 4 4752
Class B Accuracy = tp+tn/tp+tn+fp+fn = 4754/4834 = 0.98
我曾尝试在线搜索,但未找到任何超过2X2矩阵的计算。对于多类情况,可使用以下方法:
代码背后的思想:这些度量在下图中以图形化的方式表示具有大量类的一般情况
import numpy as np
cnf_matrix = np.array([[13, 0, 0],
[ 0, 10, 6],
[ 0, 0, 9]])
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)