Confusion Matrix Error: Error: `data` and `reference` should be factors with the same levels

Confusion Matrix Error: Error: `data` and `reference` should be factors with the same levels

我目前正在尝试构建一个神经网络来预测人们在数据中的排名。

排名系统是:A,B,C,D,E

在我得到混淆矩阵之前,一切都运行得非常顺利。我收到错误 "Error: data and reference should be factors with the same levels."。我在其他帖子上尝试了很多不同的方法,但 none 似乎有效。

NNPredicitions 和 test$Rank 中的级别相同。我用 table().

检查了它们
library(readxl)
library(caret)
library(neuralnet)
library(forecast)
library(tidyverse)
library(ggplot2)



Indirect <-read_excel("C:/Users/Abdulazizs/Desktop/Projects/Indirect/FIltered Indirect.xlsx", 
    n_max = 500)

Indirect$Direct_or_Indirect <- NULL


Indirect$parentaccount <- NULL


sum(is.na(Indirect))


counts <- table(Indirect$Rank)



barplot(counts)

summary(counts)



part2 <- createDataPartition(Indirect$Rank, times = 1, p = .8, list = FALSE, groups = min(5, length(Indirect$Rank)))

train <- Indirect[part2, ]
test <- Indirect[-part2, ]

set.seed(1234)

TrainingParameters <- trainControl(method = "repeatedcv", number = 10, repeats=10)

as.data.frame(train)
as.data.frame(test)

NNModel <- train(train[,-7], train$Rank,
                  method = "nnet",
                  trControl= TrainingParameters,
                  preProcess=c("scale","center"),
                  na.action = na.omit
)

NNPredictions <-predict(NNModel, test, type = "raw")



summary(NNPredictions)





confusionMatrix(NNPredictions, test$Rank)

长度(NN预测) 长度(测试$等级)

length(NNPredictions) [1] 98 length(test$Rank) [1] 98

table(NNPredictions, test$Rank, useNA="ifany") NN 预测 A B C D E 1 0 0 0 0 乙 0 6 0 0 0 C 0 0 11 0 0 0 0 0 18 0 E 0 0 0 0 62

同时将 method = "prob" 更改为 method = "raw"

Table1 <- table(NNPredictions, test$Rank, useNA = "ifany")

cnf1 <- 混淆矩阵(表 1)

dclarson 提供的答案