我目前正在尝试构建一个神经网络来预测人们在数据中的排名。
等级系统为:A、B、C、D、E
一切都运行得非常顺利,直到我到达我的混淆矩阵。我收到错误“错误:
data
和 reference
应该是具有相同水平的因子。”。我在其他帖子上尝试了很多不同的方法,但似乎都不起作用。
NNPredictions 和 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预测) 长度(测试$排名)
长度(NN预测) [1] 98 长度(测试$排名) [1] 98
表(NNPredictions,测试$Rank,useNA =“ifany”) NN预测 A B C D E 1 0 0 0 0 乙 0 6 0 0 0 0 0 11 0 0 d 0 0 0 18 0 E 0 0 0 0 62
同时将
method = "prob"
更改为 method = "raw"
Table1 <- table(NNPredictions, test$Rank, useNA = "ifany")
cnf1 <- confusionMatrix(Table1)
dclarson 提供的答案