【问题标题】:Confusion Matrix Error: Error: `data` and `reference` should be factors with the same levels混淆矩阵错误:错误:`data` 和 `reference` 应该是具有相同级别的因素
【发布时间】:2019-11-21 12:25:53
【问题描述】:

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

等级系统为: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)

长度(NNPredictions) 长度(测试$Rank)

长度(NNPredictions) [1] 98 长度(测试$排名) [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 D 0 0 0 18 0 E 0 0 0 0 62

【问题讨论】:

  • table(NNPredictions, test$Rank, useNA="ifany") 告诉你什么?
  • @dclarson 它显示:表中的错误(NNPredictions, test$Rank, useNA = "ifany") :所有参数必须具有相同的长度
  • 那是你的问题。什么是长度(NNPredictions)和长度(test$Rank)?它们必须相同。如果不是,您可能在 train$Rank 中缺少值。
  • 我将 type = "prob" 更改为 type = "raw"。检查了长度,两者都有 98 个级别。
  • 将其更改为“原始”允许我使用表(NNPredictions,test$Rank,useNA="ifany")。结果在上面列出

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


【解决方案1】:

还将 method = "prob" 更改为 method = "raw"

Table1

cnf1

由 dclarson 提供回答

【讨论】:

    猜你喜欢
    • 2021-03-18
    • 2020-09-17
    • 2020-08-17
    • 1970-01-01
    • 2020-03-16
    • 2021-12-20
    • 2019-01-04
    • 2023-03-21
    • 2021-09-20
    相关资源
    最近更新 更多