用 R 中的颜色和频率绘制混淆矩阵 [英] Plot A Confusion Matrix with Color and Frequency in R

查看:66
本文介绍了用 R 中的颜色和频率绘制混淆矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个混淆矩阵,但是,我不想只使用热图,因为我认为它们的数值分辨率很差.相反,我还想绘制正方形中间的频率.例如,我喜欢这个输出:

I want to plot a confusion matrix, but, I don't want to just use a heatmap, because I think they give poor numerical resolution. Instead, I want to also plot the frequency in the middle of the square. For instance, I like the output of this:

library(mlearning);
data("Glass", package = "mlbench")
Glass$Type <- as.factor(paste("Glass", Glass$Type))

summary(glassLvq <- mlLvq(Type ~ ., data = Glass));
(glassConf <- confusion(predict(glassLvq, Glass, type = "class"), Glass$Type))

plot(glassConf) # Image by default

但是,1.) 我不明白01、02 等"的意思是沿着每个轴.我们怎样才能摆脱它?2.) 我希望预测"作为y"维度的标签,实际"作为x"维度的标签3.) 我想用频率/概率替换绝对计数.

However, 1.) I don't understand that the "01, 02, etc" means along each axis. How can we get rid of that? 2.) I would like 'Predicted' to be as the label of the 'y' dimension, and 'Actual' to be as the label for the 'x' dimension 3.) I would like to replace absolute counts by frequency / probability.

或者,是否有其他软件包可以做到这一点?

Alternatively, is there another package that will do this?

本质上,我希望在 R 中实现:

In essence, I want this in R:

http://www.mathworks.com/help/版本/R2013b/nnet/gs/gettingstarted_nprtool_07.gif

或:

http://c431376.r76.cf2.rackcdn.com/8805/fnhum-05-00189-HTML/image_m/fnhum-05-00189-g009.jpg

推荐答案

mlearning 包在绘制混淆矩阵时似乎非常不灵活.

The mlearning package seems quite inflexible with plotting confusion matrices.

从你的 glassConf 对象开始,你可能想要做这样的事情:

Starting with your glassConf object, you probably want to do something like this:

prior(glassConf) <- 100 
# The above rescales the confusion matrix such that columns sum to 100.
opar <- par(mar=c(5.1, 6.1, 2, 2))
x <- x.orig <- unclass(glassConf)
x <- log(x + 0.5) * 2.33
x[x < 0] <- NA
x[x > 10] <- 10
diag(x) <- -diag(x)
image(1:ncol(x), 1:ncol(x),
      -(x[, nrow(x):1]), xlab='Actual', ylab='',
      col=colorRampPalette(c(hsv(h = 0, s = 0.9, v = 0.9, alpha = 1), 
                             hsv(h = 0, s = 0, v = 0.9, alpha = 1), 
                             hsv(h = 2/6, s = 0.9, v = 0.9, alpha = 1)))(41), 
      xaxt='n', yaxt='n', zlim=c(-10, 10))
axis(1, at=1:ncol(x), labels=colnames(x), cex.axis=0.8)
axis(2, at=ncol(x):1, labels=colnames(x), las=1, cex.axis=0.8)
title(ylab='Predicted', line=4.5)
abline(h = 0:ncol(x) + 0.5, col = 'gray')
abline(v = 0:ncol(x) + 0.5, col = 'gray')
text(1:6, rep(6:1, each=6), 
     labels = sub('^0$', '', round(c(x.orig), 0)))
box(lwd=2)
par(opar) # reset par

上面的代码使用了plot.confusion调用的confusionImage函数的一部分.

The above code uses bits and pieces of the confusionImage function called by plot.confusion.

这篇关于用 R 中的颜色和频率绘制混淆矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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