R, Confusion Matrix in percent

5.1k views Asked by At

In R how to get Confusion Matrix in percent (or fraction of 1). The "caret" package provides useful function but shows absolute number of samples.

library(caret)
data(iris)
T <- iris$Species
P <- sample(iris$Species)
confusionMatrix(P, T)
Confusion Matrix and Statistics
             Reference
Prediction   setosa versicolor virginica
setosa         15         16        19
versicolor     19         16        15
virginica      16         18        16
1

There are 1 answers

0
cdeterman On

The caret function is nice if you want all the summary statistics. If all you care about is the 'percentage' confusion matrix, you can just use prop.table and table. Also, for future reference, strictly programming questions should be posted to stackoverflow not CrossValidated.

prop.table(table(P,T))
> prop.table(table(P,T))
            T
P                setosa versicolor  virginica
  setosa     0.11333333 0.10666667 0.11333333
  versicolor 0.09333333 0.13333333 0.10666667
  virginica  0.12666667 0.09333333 0.11333333

If you would like to keep the summary statistics from caret, just you prop.table on the confusion matrix object.

prop.table(caret::confusionMatrix(P,T)$table)