首先,您的代码中有遗漏-为了运行,我需要添加以下命令:
import keras(x_train, y_train), (x_test, y_test) = mnist.load_data()
完成此 *** 作并给出混淆矩阵
cm1:
array([[ 965, 0, 1, 0, 0, 2, 6, 1, 5, 0], [ 0, 1113, 4, 2, 0, 0, 3, 0, 13, 0], [ 8, 0, 963, 14, 5, 1, 7, 8, 21, 5], [ 0, 0, 3, 978, 0, 7, 0, 6, 12, 4], [ 1, 0, 4, 0, 922, 0, 9, 3, 3, 40], [ 4, 1, 1, 27, 0, 824, 6, 1, 20, 8], [ 11, 3, 1, 1, 5, 6, 925, 0, 6, 0], [ 2, 6, 17, 8, 2, 0, 1, 961, 2, 29], [ 5, 1, 2, 13, 4, 6, 2, 6, 929, 6], [ 6, 5, 0, 7, 5, 6, 1, 6, 10, 963]])
这是您如何获取 每类 要求的TP,FP,FN,TN的方法:
真实肯定只是对角线元素:
TruePositive = np.diag(cm1)TruePositive# array([ 965, 1113, 963, 978, 922, 824, 925, 961, 929, 963])
误报是各列的总和,减去对角线元素:
FalsePositive = []for i in range(num_classes): FalsePositive.append(sum(cm1[:,i]) - cm1[i,i])FalsePositive# [37, 16, 33, 72, 21, 28, 35, 31, 92, 92]
同样,False Negatives是相应行的总和,减去对角线元素:
FalseNegative = []for i in range(num_classes): FalseNegative.append(sum(cm1[i,:]) - cm1[i,i])FalseNegative# [15, 22, 69, 32, 60, 68, 33, 67, 45, 46]
现在,“真否定论”有些棘手;让我们首先考虑一个真正的否定词,相对于class
0来说是什么意思:它表示所有被正确识别为
_not
0_的样本。因此,本质上我们应该做的是从混淆矩阵中删除相应的行和列,然后对所有剩余元素求和:
TrueNegative = []for i in range(num_classes): temp = np.delete(cm1, i, 0) # delete ith row temp = np.delete(temp, i, 1) # delete ith column TrueNegative.append(sum(sum(temp)))TrueNegative# [8998, 8871, 9004, 8950, 9057, 9148, 9040, 9008, 8979, 8945]
让我们进行完整性检查:对于 每个类 ,TP,FP,FN和TN的总和必须等于测试集的大小(此处为10,000):让我们确认确实如此:
l = len(y_test)for i in range(num_classes): print(TruePositive[i] + FalsePositive[i] + FalseNegative[i] + TrueNegative[i] == l)
结果是
TrueTrueTrueTrueTrueTrueTrueTrueTrueTrue
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)