在 R 中绘制带有颜色和频率的混淆矩阵

2024-01-15

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

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.)我想用频率/概率替换绝对计数。

或者,是否有另一个包可以做到这一点?

本质上,我希望在 R 中做到这一点:

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

OR:

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


The mlearning包在绘制混淆矩阵方面似乎相当不灵活。

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

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

上面的代码使用了confusionImage函数调用者plot.confusion.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 R 中绘制带有颜色和频率的混淆矩阵 的相关文章

随机推荐