Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

r - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...