R error: all arguments must have the same length
R error: all arguments must have the same length
我在用 R 做朴素贝叶斯时出错,这是我的代码和错误
library(e1071)
#data
train_data <- read.csv('https://raw.githubusercontent.com/JonnyyJ/data/master/train.csv',header=T)
test_data <- read.csv('https://raw.githubusercontent.com/JonnyyJ/data/master/test.csv',header=T)
efit <- naiveBayes(y~job+marital+education+default+contact+month+day_of_week+
poutcome+age+pdays+previous+cons.price.idx+cons.conf.idx+euribor3m
,train_data)
pre <- predict(efit, test_data)
bayes_table <- table(pre, test_data[,ncol(test_data)])
accuracy_test_bayes <- sum(diag(bayes_table))/sum(bayes_table)
list('predict matrix'=bayes_table, 'accuracy'=accuracy_test_bayes)
错误:
bayes_table <- table(pre, test_data[,ncol(test_data)])
Error in table(pre, test_data[, ncol(test_data)]) :
all arguments must have the same length
accuracy_test_bayes <- sum(diag(bayes_table))/sum(bayes_table)
Error in diag(bayes_table) : object 'bayes_table' not found
list('predict matrix'=bayes_table, 'accuracy'=accuracy_test_bayes)
Error: object 'bayes_table' not found
我真的不明白这是怎么回事,因为我是R新手
出于某种原因,默认值 predict(efit, test_data, type = "class")
在这种情况下不起作用(可能是因为您的模型预测测试数据集中的所有观察结果为 0
)。您还需要使用您的结果构建 table(即 test_data[,ncol(test_data)]
returns euribor3m
)。以下应该有效:
pre <- predict(efit, test_data, type = "raw") %>%
as.data.frame() %>%
mutate(prediction = if_else(0 < 1, 0, 1)) %>%
pull(prediction)
bayes_table <- table(pre, test_data$y)
accuracy_test_bayes <- sum(diag(bayes_table)) / sum(bayes_table)
list('predict matrix' = bayes_table, 'accuracy' = accuracy_test_bayes)
# $`predict matrix`
#
# pre 0 1
# 0 7282 956
#
# $accuracy
# [1] 0.8839524
我在用 R 做朴素贝叶斯时出错,这是我的代码和错误
library(e1071)
#data
train_data <- read.csv('https://raw.githubusercontent.com/JonnyyJ/data/master/train.csv',header=T)
test_data <- read.csv('https://raw.githubusercontent.com/JonnyyJ/data/master/test.csv',header=T)
efit <- naiveBayes(y~job+marital+education+default+contact+month+day_of_week+
poutcome+age+pdays+previous+cons.price.idx+cons.conf.idx+euribor3m
,train_data)
pre <- predict(efit, test_data)
bayes_table <- table(pre, test_data[,ncol(test_data)])
accuracy_test_bayes <- sum(diag(bayes_table))/sum(bayes_table)
list('predict matrix'=bayes_table, 'accuracy'=accuracy_test_bayes)
错误:
bayes_table <- table(pre, test_data[,ncol(test_data)]) Error in table(pre, test_data[, ncol(test_data)]) : all arguments must have the same length accuracy_test_bayes <- sum(diag(bayes_table))/sum(bayes_table) Error in diag(bayes_table) : object 'bayes_table' not found list('predict matrix'=bayes_table, 'accuracy'=accuracy_test_bayes) Error: object 'bayes_table' not found
我真的不明白这是怎么回事,因为我是R新手
出于某种原因,默认值 predict(efit, test_data, type = "class")
在这种情况下不起作用(可能是因为您的模型预测测试数据集中的所有观察结果为 0
)。您还需要使用您的结果构建 table(即 test_data[,ncol(test_data)]
returns euribor3m
)。以下应该有效:
pre <- predict(efit, test_data, type = "raw") %>%
as.data.frame() %>%
mutate(prediction = if_else(0 < 1, 0, 1)) %>%
pull(prediction)
bayes_table <- table(pre, test_data$y)
accuracy_test_bayes <- sum(diag(bayes_table)) / sum(bayes_table)
list('predict matrix' = bayes_table, 'accuracy' = accuracy_test_bayes)
# $`predict matrix`
#
# pre 0 1
# 0 7282 956
#
# $accuracy
# [1] 0.8839524