Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct the confusion matrix for a multi class variable

Suppose I have a factor variable y with n levels, for which I have both predictions and real outcomes available. How can I construct the confusion matrix?

set.seed(12345)
y_actual = as.factor(sample(c('A','B', 'C', 'D', 'E'), 100, replace = TRUE))
set.seed(12346)
y_predict = as.factor(sample(c('A','B', 'C', 'D', 'E'), 100, replace = TRUE))

This question is already answered for the case n = 2. See

R: how to make a confusion matrix for a predictive model?

What I tried

This is how far I got

ones = data.frame(total = rep(1,100));
confusion = aggregate(ones, list(Prediction = predict, Reality = real), sum, a.action=0)
confusion

  Prediction Reality total
1          A       A    12
2          B       A     5
3          C       A    15
4          A       B    15
5          B       B     7
6          C       B     8
7          A       C    12
8          B       C    16
9          C       C    10

Now this has to be brought in the shape of a matrix.

Background

The confusion matrix has as the horizontal label "actual class" and as vertical label "predicted class". The matrix elements are simply counts like this:

element (1,1) = Number of counts for actual class is A and predicted class is A

element (1,2) = Number of counts for actual class is A and predicted class is B

etc

like image 295
sjdh Avatar asked Dec 03 '22 19:12

sjdh


1 Answers

You should be able to do what you want with table:

table(y_actual, y_predict)
#         y_predict
# y_actual A B C D E
#        A 4 3 4 2 8
#        B 7 1 3 6 2
#        C 3 7 1 0 4
#        D 3 6 6 4 6
#        E 6 5 5 1 3
like image 69
A5C1D2H2I1M1N2O1R2T1 Avatar answered Dec 21 '22 23:12

A5C1D2H2I1M1N2O1R2T1