How do you calculate precision and recall for multiclass classification using confusion matrix? 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.
 A: In a 2-hypothesis case, the confusion matrix is usually:





Declare H1
Declare H0




Is H1
TP
FN


Is H0
FP
TN




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.
Edit
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$.
A: 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.

A: @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')

A: Using sklearn or tensorflow and numpy:
from sklearn.metrics import confusion_matrix
# or: 
# from tensorflow.math 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
np.mean(recall)
np.mean(precision)

A: Here is a different view from the other answers that I think will be helpful to others. The goal here is to allow you to compute these metrics using basic laws of probability.
First, it helps to understand what a confusion matrix is telling us in general. Let $Y$ represent a class label and $\hat Y$ represent a class prediction. In the binary case, let the two possible values for $Y$ and $\hat Y$ be $0$ and $1$, which represent the classes. Next, suppose that the confusion matrix for $Y$ and $\hat Y$ is:





$\hat Y = 0$
$\hat Y = 1$




$Y = 0$
10
20


$Y = 1$
30
40




With hindsight, let us normalize the rows and columns of this confusion matrix, such that the sum of all elements of the confusion matrix is $1$. Currently, the sum of all elements of the confusion matrix is $10 + 20 + 30 + 40 = 100$, which is our normalization factor. After dividing the elements of the confusion matrix by the normalization factor, we get the following normalized confusion matrix:





$\hat Y = 0$
$\hat Y = 1$




$Y = 0$
$\frac{1}{10}$
$\frac{2}{10}$


$Y = 1$
$\frac{3}{10}$
$\frac{4}{10}$




With this formulation of the confusion matrix, we can interpret $Y$ and $\hat Y$ slightly differently. We can interpret them as jointly Bernoulli (binary) random variables, where their normalized confusion matrix represents their joint probability mass function. When we interpret $Y$ and $\hat Y$ this way, the definitions of precision and recall are much easier to remember using Bayes' rule and the law of total probability:
\begin{align}
\text{Precision} &= P(Y = 1 \mid \hat Y = 1) = \frac{P(Y = 1 , \hat Y = 1)}{P(Y = 1 , \hat Y = 1) + P(Y = 0 , \hat Y = 1)} \\
\text{Recall} &= P(\hat Y = 1 \mid Y = 1) = \frac{P(Y = 1 , \hat Y = 1)}{P(Y = 1 , \hat Y = 1) + P(Y = 1 , \hat Y = 0)}
\end{align}
How do we determine these probabilities? We can estimate them using the normalized confusion matrix. From the table above, we see that
\begin{align}
P(Y = 0 , \hat Y = 0) &\approx \frac{1}{10} \\
P(Y = 0 , \hat Y = 1) &\approx \frac{2}{10} \\
P(Y = 1 , \hat Y = 0) &\approx \frac{3}{10} \\
P(Y = 1 , \hat Y = 1) &\approx \frac{4}{10}
\end{align}
Therefore, the precision and recall for this specific example are
\begin{align}
\text{Precision} &= P(Y = 1 \mid \hat Y = 1) = \frac{\frac{4}{10}}{\frac{4}{10} + \frac{2}{10}} = \frac{4}{4 + 2} = \frac{2}{3} \\
\text{Recall} &= P(\hat Y = 1 \mid Y = 1) = \frac{\frac{4}{10}}{\frac{4}{10} + \frac{3}{10}} = \frac{4}{4 + 3} = \frac{4}{7}
\end{align}
Note that, from the calculations above, we didn't really need to normalize the confusion matrix before computing the precision and recall. The reason for this is that, because of Bayes' rule, we end up dividing one value that is normalized by another value that is normalized, which means that the normalization factor can be cancelled out.
A nice thing about this interpretation is that it can be generalized to confusion matrices of any size. In the case where there are more than 2 classes, $Y$ and $\hat Y$ are no longer considered to be jointly Bernoulli, but rather jointly categorical. Moreover, we would need to specify which class we are computing the precision and recall for. In fact, the definitions above may be interpreted as the precision and recall for class $1$. We can also compute the precision and recall for class $0$, but these have different names in the literature.
