ROCR error: Format of predictions is invalid

ROCR error: Format of predictions is invalid

从 glmnet 获得我的预测后,我尝试使用 "ROCR" 包中的 "prediction" 函数来获取 tpr、fpr 等,但出现此错误:

pred <- prediction(pred_glmnet_s5_3class, y)
Error in prediction(pred_glmnet_s5_3class, y) : 
Format of predictions is invalid.

我已经输出了 glmnet 预测和标签,它们看起来格式相似,因此我不明白这里有什么无效的。

代码如下,输入可以在这里找到input。这是一个小数据集,应该不会花太多时间 运行。

library("ROCR")
library("caret")
sensor6data_s5_3class <- read.csv("/home/sensei/clustering /sensor6data_f21_s5_with3Labels.csv")
sensor6data_s5_3class <- within(sensor6data_s5_3class, Class <- as.factor(Class))
sensor6data_s5_3class$Class2 <- relevel(sensor6data_s5_3class$Class,ref="1")

set.seed("4321")
inTrain_s5_3class <- createDataPartition(y = sensor6data_s5_3class$Class, p = .75, list = FALSE)
training_s5_3class <- sensor6data_s5_3class[inTrain_s5_3class,]
testing_s5_3class <- sensor6data_s5_3class[-inTrain_s5_3class,] 
y <- testing_s5_3class[,22]

ctrl_s5_3class <- trainControl(method = "repeatedcv", number = 10, repeats = 10 , savePredictions = TRUE)
model_train_glmnet_s5_3class <- train(Class2 ~ ZCR + Energy + SpectralC + SpectralS + SpectralE + SpectralF + SpectralR + MFCC1 + MFCC2 + MFCC3 + MFCC4 + MFCC5 + MFCC6 + MFCC7 + MFCC8 + MFCC9 + MFCC10 + MFCC11 + MFCC12 + MFCC13, data = training_s5_3class, method="glmnet", trControl = ctrl_s5_3class)
pred_glmnet_s5_3class = predict(model_train_glmnet_s5_3class, newdata=testing_s5_3class, s = "model_train_glmnet_s5_3class$finalModel$lambdaOpt")

pred <- prediction(pred_glmnet_s5_3class, y)

感谢您的帮助!

主要问题是 predictionpredictionslabels 参数都采用 "a vector, matrix, list, or data frame"。尽管 pred_glmnet_s5_3classy 看起来像向量,但它们不是,例如

sapply(c(is.vector, is.matrix, is.list, is.data.frame), do.call, list(y))
# [1] FALSE FALSE FALSE FALSE

其实是因子(从例class(y)可以看出),?is.vector告诉我们

Note that factors are not vectors; ‘is.vector’ returns ‘FALSE’ and ‘as.vector’ converts a factor to a character vector for ‘mode = "any"’.

我们可以将两个对象都转换为 numeric:

pred <- prediction(as.numeric(pred_glmnet_s5_3class), as.numeric(y))
#   Number of classes is not equal to 2.
# ROCR currently supports only evaluation of binary classification tasks.

不幸的是,它产生了一个不同的问题,超出了这个问题的范围。