繁体   English   中英

当未使用R中的插入符号包预测所有类别时的混淆矩阵

[英]Confusion matrix when all classes are not predicted with caret package in R

我有一个分类模型predict(model, test.x)来评估具有11个类的数据的模型,预测的结果是:

table(predicted_class)
0   1   2   3   5   6   8  10 
7   6  232  11  74  58   1   1 

我的测试标签(真相)是:

table(test.y)
  0   1   2   3   4   5   6   7   8   9  10 
105  16  78  25  14  74  12   9  23  15  19 

当我想使用插入符号包获取混淆矩阵时,出现此错误消息,因为模型未预测类7和9:

caret::confusionMatrix(test.y, predicted_class, mode = "everything")
Error in confusionMatrix.default(test.y, predicted_class,  : 
  the data cannot have more levels than the reference

当预测中缺少某些因子水平时,如何获得混淆矩阵:如何为缺少的类(在本例中为4、7和9)的预测类自动添加0

通过结合union因素使水平相同

all_class <- union(predicted_class, test.y)
newtable <- table(factor(predicted_class, all_class), factor(test.y, all_class))
caret::confusionMatrix(newtable)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM