0 votes
1 view
in AI and Deep Learning by (42.1k points)

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:

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", 





predictionsTree <- predict(treeFit, testdata)

confusionMatrix(predictionsTree, testdata$catgeory)




The error occurs when generating the confusion matrix. The levels are the same on both objects. I can’t 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!!

> strpred)

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)


> levels(testing$Final)


1 Answer

0 votes
by (90.8k points)
edited by

Whenever you try to build a confusion matrix, make sure that both the true values and the prediction values are of “factor” data-type.

Here both pred and testing$Final must be of datatype factor. Here testing$Final is of type int, convert it to factor and then build the confusion matrix.

confusionMatrix(factor(pred, levels=1:490), factor(testing$final, levels=1:490))

We have to keep in mind that both levels should be the same.

table(factor(pred, levels=min(test):max(test)), factor(test, levels=min(test):max(test)))// table is name the confusion matrix

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

You can also read the Artificial Intelligence Tutorial and join AI Course to get a sparkling start for your AI journey.

If you want to make your career in Artificial Intelligence then go through this video:

Welcome to Intellipaat Community. Get your technical queries answered by top developers !