I wonder how to compute precision and recall using a confusion matrix for a multi-class classification problem. Specifically, an observation can only be assigned to its most probable class / label. I would like to compute:

  • Precision = TP / (TP+FP)
  • Recall = TP / (TP+FN)

for each class, and then compute the micro-averaged F-measure.

  • $\begingroup$ This docx, Evaluating a classification model – What does precision and recall tell me?, from Compumine provides a simple introduction to the confusion matrix and the measures derived from it. It helps to create the confusion matrix, precision, recall, specificity and accuracy. $\endgroup$ Jul 8, 2013 at 3:56
  • 4
    $\begingroup$ find the answer here. Very good explanation youtube.com/watch?v=FAr2GmWNbT0 $\endgroup$
    – user73392
    Apr 13, 2015 at 2:53
  • $\begingroup$ The Compumine link is dead. $\endgroup$
    – Trenton
    Jun 11, 2015 at 3:52
  • $\begingroup$ For multiclass case, what I understand that along the rows (axis=0) is the recall and along the columns (axis=1) is the precision. rxnlp.com/… $\endgroup$ Mar 26, 2019 at 12:58

4 Answers 4


In a 2-hypothesis case, the confusion matrix is usually:

Declare H1 Declare H0

where I've used something similar to your notation:

  • TP = true positive (declare H1 when, in truth, H1),
  • FN = false negative (declare H0 when, in truth, H1),
  • FP = false positive
  • TN = true negative

From the raw data, the values in the table would typically be the counts for each occurrence over the test data. From this, you should be able to compute the quantities you need.


The generalization to multi-class problems is to sum over rows / columns of the confusion matrix. Given that the matrix is oriented as above, i.e., that a given row of the matrix corresponds to specific value for the "truth", we have:

$\text{Precision}_{~i} = \cfrac{M_{ii}}{\sum_j M_{ji}}$

$\text{Recall}_{~i} = \cfrac{M_{ii}}{\sum_j M_{ij}}$

That is, precision is the fraction of events where we correctly declared $i$ out of all instances where the algorithm declared $i$. Conversely, recall is the fraction of events where we correctly declared $i$ out of all of the cases where the true of state of the world is $i$.

  • 1
    $\begingroup$ In my case, there are 10+ classes, so I guess the FN will mean the total count of declare class H(i), i != 1; and the same is FP? $\endgroup$
    – daiyue
    Mar 4, 2013 at 17:14
  • $\begingroup$ Hi, I wonder what the values will be for the Precision and Recall, if TP+FP=0, and TP+FN=0 for some actual class in the confusion matrix. $\endgroup$
    – daiyue
    Mar 13, 2013 at 15:35
  • $\begingroup$ The precision for class i is undefined if there are no instances where the algorithm declares i. The recall for class i is undefined if the test set does not include class i. $\endgroup$
    – Dave
    Mar 13, 2013 at 18:27
  • $\begingroup$ My final goal is calculate the Macro F Measure, so I need precision and recall values for each class i; so how can I compute the Macro-F measure if the above two cases appear in some class i? In particular, what's the value for Fi, and does class i count as one of the classes M, that the number of elements in M will be count as the denominator of the formula for calculating Macro F measure. $\endgroup$
    – daiyue
    Mar 13, 2013 at 20:18
  • 1
    $\begingroup$ sry, could you explain your idea more clearly? $\endgroup$
    – daiyue
    Mar 13, 2013 at 22:54

Good summary paper, looking at these metrics for multi-class problems:

  • Sokolova, M., & Lapalme, G. (2009). A systematic analysis of performance measures for classification tasks. Information Processing and Management, 45, p. 427-437. (pdf)

The abstract reads:

This paper presents a systematic analysis of twenty four performance measures used in the complete spectrum of Machine Learning classification tasks, i.e., binary, multi-class, multi-labelled, and hierarchical. For each classification task, the study relates a set of changes in a confusion matrix to specific characteristics of data. Then the analysis concentrates on the type of changes to a confusion matrix that do not change a measure, therefore, preserve a classifier’s evaluation (measure invariance). The result is the measure invariance taxonomy with respect to all relevant label distribution changes in a classification problem. This formal analysis is supported by examples of applications where invariance properties of measures lead to a more reliable evaluation of classifiers. Text classification supplements the discussion with several case studies.

  • 2
    $\begingroup$ Welcome to the site, @JamesTaylor. Would you mind giving a summary of the info in the linked paper to help readers decide whether it's what they need & in case the link goes dead? $\endgroup$ Mar 12, 2013 at 16:27

Using sklearn and numpy:

from sklearn.metrics import confusion_matrix
import numpy as np

labels = ...
predictions = ...

cm = confusion_matrix(labels, predictions)
recall = np.diag(cm) / np.sum(cm, axis = 1)
precision = np.diag(cm) / np.sum(cm, axis = 0)

To get overall measures of precision and recall, use then


@Cristian Garcia code can be reduced by sklearn.

>>> from sklearn.metrics import precision_score
>>> y_true = [0, 1, 2, 0, 1, 2]
>>> y_pred = [0, 2, 1, 0, 0, 1]
>>> precision_score(y_true, y_pred, average='micro')

Not the answer you're looking for? Browse other questions tagged or ask your own question.