混淆矩阵中的错误:数据和参考因子必须具有相同数量的级别 [英] Error in Confusion Matrix : the data and reference factors must have the same number of levels

查看:135
本文介绍了混淆矩阵中的错误:数据和参考因子必须具有相同数量的级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用R插入符号训练了线性回归模型.我现在正在尝试生成一个混淆矩阵,并不断出现以下错误:

I've trained a Linear Regression model with R caret. I'm now trying to generate a confusion matrix and keep getting the following error:

confusionMatrix.default(pred,testing $ Final)中的错误: 数据和参考因子必须具有相同数量的水平

Error in confusionMatrix.default(pred, testing$Final) : the data and reference factors must have the same number of levels

EnglishMarks <- read.csv("E:/Subject Wise Data/EnglishMarks.csv", 
header=TRUE)
inTrain<-createDataPartition(y=EnglishMarks$Final,p=0.7,list=FALSE)
training<-EnglishMarks[inTrain,]
testing<-EnglishMarks[-inTrain,]
predictionsTree <- predict(treeFit, testdata)
confusionMatrix(predictionsTree, testdata$catgeory)
modFit<-train(Final~UT1+UT2+HalfYearly+UT3+UT4,method="lm",data=training)
pred<-format(round(predict(modFit,testing)))              
confusionMatrix(pred,testing$Final)

生成混淆矩阵时发生错误.两个对象的级别相同.我不知道是什么问题.它们的结构和水平在下面给出.它们应该是相同的.任何帮助将不胜感激,因为它使我无法自拔!

The error occurs when generating the confusion matrix. The levels are the same on both objects. I cant figure out what the problem is. Their structure and levels are given below. They should be the same. Any help would be greatly appreciated as its making me cracked!!

> str(pred)
chr [1:148] "85" "84" "87" "65" "88" "84" "82" "84" "65" "78" "78" "88" "85"  
"86" "77" ...
> str(testing$Final)
int [1:148] 88 85 86 70 85 85 79 85 62 77 ...

> levels(pred)
NULL
> levels(testing$Final)
NULL

推荐答案

执行table(pred)table(testing$Final).您会看到测试集中至少有一个数字永远不会被预测(即在pred中永远不会出现).这就是为什么级别数不同"的原因. 此处.

Do table(pred) and table(testing$Final). You will see that there is at least one number in the testing set that is never predicted (i.e. never present in pred). This is what is meant why "different number of levels". There is an example of a custom made function to get around this problem here.

但是,我发现这个技巧很好用:

However, I found that this trick works fine:

table(factor(pred, levels=min(test):max(test)), 
      factor(test, levels=min(test):max(test)))

它应该为您提供与该函数完全相同的混淆矩阵.

It should give you exactly the same confusion matrix as with the function.

这篇关于混淆矩阵中的错误:数据和参考因子必须具有相同数量的级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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