Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R, Confusion Matrix in percent

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
like image 221
d.putto Avatar asked Nov 17 '14 12:11

d.putto


1 Answers

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)
like image 150
cdeterman Avatar answered Sep 19 '22 16:09

cdeterman