Я пытаюсь составить дерево решений, но эта ошибка возникает, когда я создаю матрицу путаницы в последней строке:
Error : `data` and `reference` should be factors with the same levels
Вот мой код:
library(rpart) library(caret) library(dplyr) library(rpart.plot) library(xlsx) library(caTools) library(data.tree) library(e1071) #Loading the Excel File library(readxl) FINALDATA <- read_excel(«Desktop/FINALDATA.xlsm») View(FINALDATA) df <- FINALDATA View(df) #Selecting the meaningful columns for prediction #df <- select(df, City, df$`Customer type`, Gender, Quantity, Total, Date, Time, Payment, Rating) df <- select(df, City, `Customer type`, Gender, Quantity, Total, Date, Time, Payment, Rating) #making sure the data is in the right format df <- mutate(df, City= as.character(City), `Customer type`= as.character(`Customer type`), Gender= as.character(Gender), Quantity= as.numeric(Quantity), Total= as.numeric(Total), Time= as.numeric(Time), Payment = as.character(Payment), Rating= as.numeric(Rating)) #Splitting into training and testing data set.seed(123) sample = sample.split(‘Customer type’, SplitRatio = .70) train = subset(df, sample==TRUE) test = subset(df, sample == FALSE) #Training the Decision Tree Classifier tree <- rpart(df$`Customer type` ~., data = train) #Predictions tree.customertype.predicted <- predict(tree, test, type= ‘class’) #confusion Matrix for evaluating the model confusionMatrix(tree.customertype.predicted, test$`Customer type`)
Итак, я попытался сделать это, как сказано в другой теме:
confusionMatrix(table(tree.customertype.predicted, test$`Customer type`))
Но у меня все равно ошибка:
Error in !all.equal(nrow(data), ncol(data)) : argument type is invalid
Просто чтобы следить. В случаях, когда у вас есть большой файл данных, можно создать образец набора данных, который воспроизводит вашу проблему. Вот некоторые рекомендации о том, как люди это делают. Наличие данных облегчает сообществу помощь вам. — person Nicolas Duaut schedule 26.02.2021
Спасибо ! Я буду применять руководство — person Nicolas Duaut schedule 26.02.2021
Источник: