如何在R中使用热图绘制混淆矩阵? [英] How to plot a confusion matrix using heatmaps in R?

查看:903
本文介绍了如何在R中使用热图绘制混淆矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个混淆矩阵,使得:

I have a confusion matrix such that:

  a b c d e f g h i j
a 5 4 0 0 0 0 0 0 0 0
b 0 0 0 0 0 0 0 0 0 0
c 0 0 4 0 0 0 0 0 0 0
d 0 0 0 0 0 0 0 0 0 0
e 2 0 0 0 2 0 0 0 0 0
f 1 0 0 0 0 2 0 0 0 0
g 0 0 0 0 0 0 0 0 0 0
h 0 0 0 0 0 0 0 0 0 0 
i 0 0 0 0 0 0 0 0 0 0 
j 0 0 0 0 0 0 0 0 0 0 

其中的字母表示班级标签.

where the letters denote the class labels.

我只需要绘制混淆矩阵.我搜索了几个工具. R中的热图看起来像我所需要的.由于我对R一无所知,因此很难对样本进行更改.如果有人能尽快帮助我如何画画,我将不胜感激.或者也欢迎任何其他建议而不是热图. 我知道有很多关于此的示例,但是我仍然无法用自己的数据进行绘制.

I just need to plot the confusion matrix. I searched a couple of tools. Heatmaps in R looks like what I need. As I don't know anything about R, it is really hard to do changes on the samples. If anybody could help me shortly how to draw, I will be really appreciated. Or any other suggestion rather than heatmaps are welcome as well. I know there is plenty of samples about this, but still I cannot manage to draw with my own data.

推荐答案

如Greg所述,image可能是解决方法:

As Greg mentioned, image is probably the way to go:

z = c(5,4,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,4,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
2,0,0,0,2,0,0,0,0,0,
1,0,0,0,0,2,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0)

z = matrix(z, ncol=10)
colnames(z) = c("a","b","c","d","e","f","g","h","i", "j")
rownames(z) = c("a","b","c","d","e","f","g","h","i", "j")

##To get the correct image plot rotation
##We need to flip the plot
image(z[,ncol(z):1], axes=FALSE)

##Add in the y-axis labels. Similar idea for x-axis.
axis(2, at = seq(0, 1, length=length(colnames(z))), labels=colnames(z))

您可能还想看看heatmap函数:

heatmap(t(z)[ncol(z):1,], Rowv=NA,
               Colv=NA, col = heat.colors(256))

这篇关于如何在R中使用热图绘制混淆矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆