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

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

问题描述

我已经用 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天全站免登陆