混淆矩阵错误:错误:“数据”和“参考”应该是具有相同水平的因素

时间:2019-07-11 18:05:03

标签: r machine-learning neural-network confusion-matrix

我目前正在尝试建立一个神经网络,以预测数据中人员的排名。

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

一切顺利,直到我进入困惑矩阵。我收到错误“错误:datareference应该是具有相同水平的因子。”。我在其他帖子上尝试了许多不同的方法,但是似乎都没有用。

在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)

length(NNPredictions) 长度(test $ Rank)

  

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

table(NNPredictions,test $ Rank,useNA =“ ifany”) NNPredictions A B C D E             A 1 0 0 0 0             B 0 6 0 0 0             C 0 0 11 0 0             D 0 0 0 18 0             E 0 0 0 0 62

1 个答案:

答案 0 :(得分:0)

也将method =“ prob”更改为method =“ raw”

表1 <-表(NNPredictions,test $ Rank,useNA =“ ifany”)

cnf1 <-confusionMatrix(表1)

dclarson提供的答案