[英]how to reorder the contingency table to form a confusion matrix in R
In terms of the application of clustering algorithm, after I got the contingency table of the counts at each combination of actual class and predicted class, for example,在聚类算法的应用方面,我得到了实际 class 和预测 class 的每个组合的计数列联表后,例如,
1 2 3
A 2 3 15
B 20 1 4
C 0 32 1
How could I write a function to get the confusion matrix that maximizes the diagonal by changing the order of columns?我怎么能写一个 function 来获得通过改变列的顺序来最大化对角线的混淆矩阵? Thanks!谢谢!
1 2 3
A 15 2 3
B 4 20 1
C 1 0 32
We could use max.col
to get the column index of the max value per row, and use that for rearranging the columns我们可以使用max.col
来获取每行最大值的列索引,并使用它来重新排列列
m2 <- m1[,max.col(m1, 'first')]
colnames(m2) <- seq_len(ncol(m2))
m2
# 1 2 3
#A 15 2 3
#B 4 20 1
#C 1 0 32
m1 <- structure(c(2L, 20L, 0L, 3L, 1L, 32L, 15L, 4L, 1L), .Dim = c(3L,
3L), .Dimnames = list(c("A", "B", "C"), c("1", "2", "3")))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.