I am using Scikit-learning and I need to calculate the True positive (TP), the False Positive (FP), the True Negative (TN) and the False Negative (FN) from a confusion matrix like this:[[2 0 3 4] [0 4 5 1] [1 0 3 2] [5 0 0 4]]我知道如何计算TP,FP和FN,但我不知道如何获得TN.有人可以告诉我吗?I know how to calculate the TP, the FP and the FN but I don't know how to get the TN. Can someone tell me?推荐答案我认为您应该以一对多"的方式对待这种多类分类(因此,每个2x2表i都可以衡量一个二进制分类问题(每个obs是否属于标签i).因此,您可以为每个标签计算TP,FP,FN,TN.I think you should treat this multi-class classification in a one-vs-the-rest manner (so each 2x2 table i measures the performance of a binary classification problem that whether each obs belongs to label i or not). Consequently, you can calculate the TP, FP, FN, TN for each individual label.import numpy as npconfusion_matrix = np.array([[2,0,3,4], [0,4,5,1], [1,0,3,2], [5,0,0,4]])def process_cm(confusion_mat, i=0, to_print=True): # i means which class to choose to do one-vs-the-rest calculation # rows are actual obs whereas columns are predictions TP = confusion_mat[i,i] # correctly labeled as i FP = confusion_mat[:,i].sum() - TP # incorrectly labeled as i FN = confusion_mat[i,:].sum() - TP # incorrectly labeled as non-i TN = confusion_mat.sum().sum() - TP - FP - FN if to_print: print('TP: {}'.format(TP)) print('FP: {}'.format(FP)) print('FN: {}'.format(FN)) print('TN: {}'.format(TN)) return TP, FP, FN, TNfor i in range(4): print('Calculating 2x2 contigency table for label{}'.format(i)) process_cm(confusion_matrix, i, to_print=True)Calculating 2x2 contigency table for label0TP: 2FP: 6FN: 7TN: 19Calculating 2x2 contigency table for label1TP: 4FP: 0FN: 6TN: 24Calculating 2x2 contigency table for label2TP: 3FP: 8FN: 3TN: 20Calculating 2x2 contigency table for label3TP: 4FP: 7FN: 5TN: 18 这篇关于Scikit学习:如何计算真负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-02 08:00