Home > database >  Easy way to extract common measures such as accuracy, precision, recall from 3x3 confusion matrix wi
Easy way to extract common measures such as accuracy, precision, recall from 3x3 confusion matrix wi

Time:11-11

enter image description here

Hi there, do anyone knows how to speed up this process. Just a easy way to extract common measures such as accuracy, precision, recall, f-score from 3x3 confusion matrix only with numpy or pandas? Any suggestions? I am attaching you a image with the my outcome.

cm =confusion_matrix.to_numpy()
acuracy_0 = np.round_(cm[0][0]/cm[3][3], 2)
acuracy_1 = np.round_(cm[1][1]/cm[3][3], 2)
acuracy_2 = np.round_(cm[2][2]/cm[3][3], 2)
acuracy = (acuracy_0, acuracy_1, acuracy_2)
col1 = np.array(acuracy)

precision_0 = np.round_(cm[0][0]/cm[3][0], 2)
precision_1 = np.round_(cm[1][1]/cm[3][1], 2)
precision_2 = np.round_(cm[2][2]/cm[3][2], 2)
precision = (precision_0, precision_1, precision_2)
col2 = np.array(precision)

recall_0 = np.round_(cm[0][0]/cm[0][3], 2)
recall_1 = np.round_(cm[1][1]/cm[1][3], 2)
recall_2 = np.round_(cm[2][2]/cm[2][3], 2)
recall = (recall_0, recall_1, recall_2)
col3 = np.array(recall)

f_score_0 = np.round_((2*precision_0*recall_0)/(precision_0   recall_0), 2)
f_score_1 = np.round_((2*precision_1*recall_1)/(precision_1   recall_1), 2)
f_score_2 = np.round_((2*precision_2*recall_2)/(precision_2   recall_2), 2)
f_score = (f_score_0, f_score_1, f_score_2)
col4 = np.array(f_score)

d = {'Acuracy': col1, 'Precision':col2, 'Recall':col3, 'F1_score':col4}
measurs = pd.DataFrame(data=d)
measurs.index.name = 'Class_Label'
measurs

CodePudding user response:

Since you are working with pandas dataframe, you can do:

# df is your confusion_matrix
# these are your classes
classes = df.columns[:-1]

# the true positives
true_positives = np.diagonal(df.loc[classes, classes])

accuracy = true_positives / df.loc['All','All']
precisions = true_positives / df.loc['All', classes]
recalls = true_positives / df.loc[classes, 'All']
fscores = 2 * precisions * recalls / (precisions   recalls)

out = pd.DataFrame({'accuracy': accuracy, 
                    'precisions': precisions, 
                    'recall': recalls,
                    'fscore': fscores
                   }, index=classes           # in case your classes are different from 0,1,2,...
                   ).round(2)                 # round all at once

Output:

   accuracy  precisions  recall  fscore
0      0.17        0.38    0.67    0.48
1      0.14        0.38    0.31    0.34
2      0.08        0.43    0.27    0.33

CodePudding user response:

For what it's worth, code like

accuracy_0 = cm[0][0]/cm[3][3]
accuracy_1 = cm[1][1]/cm[3][3]
accuracy_2 = cm[2][2]/cm[3][3]
accuracy = (accuracy_0, accuracy_1, accuracy_2)

can be replaced with the more concise

accuracy = cm.diagonal()[:-1]/cm[-1,-1]

so you might rewrite your code as

cm = confusion_matrix.to_numpy()
diag = cm.diagonal()[:-1]

accuracy  = diag / cm[ -1, -1]
precision = diag / cm[  3,:-1]
recall    = diag / cm[:-1,  3]
f_score   = 2 * precision * recall / (precision   recall)

out = pd.DataFrame({'Accuracy': accuracy, 
                    'Precision': precision, 
                    'Recall': recall,
                    'F-score': f_score}).round(2)
  • Related