Error in ConfusionMatrix the data and reference factors must have the same number of levels

后端 未结 9 977
自闭症患者
自闭症患者 2020-12-08 20:13

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

Error in confusionMatrix

相关标签:
9条回答
  • 2020-12-08 21:06

    Might be there are missing values in the testdata, Add the following line before "predictionsTree <- predict(treeFit, testdata)" to remove NAs. I had the same error and now it works for me.

    testdata <- testdata[complete.cases(testdata),]
    
    0 讨论(0)
  • 2020-12-08 21:07

    Maybe your model is not predicting a certain factor. Use the table() function instead of confusionMatrix() to see if that is the problem.

    0 讨论(0)
  • 2020-12-08 21:12

    Change them into a data frame and then use them in confusionMatrix function:

    pridicted <- factor(predict(treeFit, testdata))
    real <- factor(testdata$catgeory)
    
    my_data1 <- data.frame(data = pridicted, type = "prediction")
    my_data2 <- data.frame(data = real, type = "real")
    my_data3 <- rbind(my_data1,my_data2)
    
    # Check if the levels are identical
    identical(levels(my_data3[my_data3$type == "prediction",1]) , levels(my_data3[my_data3$type == "real",1]))
    
    confusionMatrix(my_data3[my_data3$type == "prediction",1], my_data3[my_data3$type == "real",1],  dnn = c("Prediction", "Reference"))
    
    0 讨论(0)
提交回复
热议问题