Accuracy, precision, and recall for multi-class model

jack picture jack · Oct 12, 2015 · Viewed 18.8k times · Source

How do I calculate accuracy, precision and recall for each class from a confusion matrix? I am using the embedded dataset iris; the confusion matrix is as below:

prediction   setosa versicolor virginica
setosa         29          0         0
versicolor      0         20         2
virginica       0          3        21

I am using 75 entries as the training set and other for testing:

iris.train <- c(sample(1:150, 75)) # have selected 75 randomly

Answer

josliber picture josliber · Oct 12, 2015

Throughout this answer, mat is the confusion matrix that you describe.

You can calculate and store accuracy with:

(accuracy <- sum(diag(mat)) / sum(mat))
# [1] 0.9333333

Precision for each class (assuming the predictions are on the rows and the true outcomes are on the columns) can be computed with:

(precision <- diag(mat) / rowSums(mat))
#     setosa versicolor  virginica 
#  1.0000000  0.9090909  0.8750000 

If you wanted to grab the precision for a particular class, you could do:

(precision.versicolor <- precision["versicolor"])
# versicolor 
#  0.9090909 

Recall for each class (again assuming the predictions are on the rows and the true outcomes are on the columns) can be calculated with:

recall <- (diag(mat) / colSums(mat))
#     setosa versicolor  virginica 
#  1.0000000  0.8695652  0.9130435 

If you wanted recall for a particular class, you could do something like:

(recall.virginica <- recall["virginica"])
# virginica 
# 0.9130435 

If instead you had the true outcomes as the rows and the predicted outcomes as the columns, then you would flip the precision and recall definitions.

Data:

(mat = as.matrix(read.table(text="  setosa versicolor virginica
 setosa         29          0         0
 versicolor      0         20         2
 virginica       0          3        21", header=T)))
#            setosa versicolor virginica
# setosa         29          0         0
# versicolor      0         20         2
# virginica       0          3        21