How do I calculate accuracy, precision and recall for each class from a Naive Bayes model? I am using the embedded dataset: iris and package tree and package e1071 for Naive Bayes. The confusion matrix is as below:
prediction setosa versicolor virginica
setosa 29 0 0
versicolor 0 20 2
virginica 0 3 21
P.S: note that i am using 75 entries as the training set and other for testing:
iris.train <- c(sample(1:150, 75)) # have selected 75 randomly
Throughout this answer,
mat
is the confusion matrix that you describe.You can calculate and store accuracy with:
Precision for each class (assuming the predictions are on the rows and the true outcomes are on the columns) can be computed with:
If you wanted to grab the precision for a particular class, you could do:
Recall for each class (again assuming the predictions are on the rows and the true outcomes are on the columns) can be calculated with:
If you wanted recall for a particular class, you could do something like:
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: