Let's pretend that
>>> y_true = [0, 0, 1, 1, 2, 0, 1] >>> y_pred = [0, 1, 0, 1, 2, 2, 1] >>> C = confusion_matrix(y_true, y_pred) >>> C array([[1, 1, 1], [1, 2, 0], [0, 0, 1]])
Then, to find out how many samples in the class got their correct label, you need to
>>> C / C.astype(np.float).sum(axis=1) array([[ 0.33333333, 0.33333333, 1. ], [ 0.33333333, 0.66666667, 0. ], [ 0. , 0. , 1. ]])
The diagonal contains the required values. Another way to calculate this is to understand that you are calculating a call to a class:
>>> from sklearn.metrics import precision_recall_fscore_support >>> _, recall, _, _ = precision_recall_fscore_support(y_true, y_pred) >>> recall array([ 0.33333333, 0.66666667, 1. ])
Similarly, if you divide the sum by axis=0 , you will get accuracy (the share of class k predictions with the truth truth label k ):
>>> C / C.astype(np.float).sum(axis=0) array([[ 0.5 , 0.33333333, 0.5 ], [ 0.5 , 0.66666667, 0. ], [ 0. , 0. , 0.5 ]]) >>> prec, _, _, _ = precision_recall_fscore_support(y_true, y_pred) >>> prec array([ 0.5 , 0.66666667, 0.5 ])