当我使用ROCR评估朴素的贝叶斯模型时,我遇到了以下错误:错误的尺寸数,真的不知道如何调试,这是我的错误日志。
$`predict matrix`
pre 0 1
0 7282 956
$accuracy
[1] 0.8839524
> #ROCR
> library(ROCR)
> pred<-prediction(predictions =pre[,2],labels =test_data$y)
Error in pre[, 2] : incorrect number of dimensions
这是我的R脚本。
library(e1071)
library(rvest)
library(dplyr)
#data
train_data <- read.csv('/Users/jonnyy/Desktop/IS/S2/IS688/project/train.csv')
test_data <- read.csv('/Users/jonnyy/Desktop/IS/S2/IS688/project/test.csv')
#construct naiveBayes model
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)
#using predict function on test data to classified prediction
pre <- predict(efit, test_data, type = "raw") %>%
as.data.frame() %>%
mutate(prediction = if_else(0 < 1, 0, 1)) %>%
pull(prediction)
#predict matrix and accuracy
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)
#ROCR
library(ROCR)
pred<-prediction(predictions =pre[,2],labels =test_data$y)
perf<-performance((pred.measure ="tpr",x.measure="fpr"))
plot(perf,main="ROC curve",col="blue",lwd=3)
abline(a=0,b=1,lwd=2,lty=2)
问题是,我的预测矩阵是2X1,似乎只是一个维度?但是当我将pre [,2]更改为pre [,1]时,它仍然无法工作