预测错误 - ROCR 包(使用概率)

Error with prediction - ROCR package (using probabilities)

我已将 "rfe" 函数与 svm 结合使用,创建了一个减少了特征的模型。然后我在输出 class 标签(二进制)、0 class 概率、1 class 概率的测试数据上使用 "predict"。然后我尝试使用预测函数,在 ROCR 包中,预测概率和真实 class 标签但得到以下错误并且我不确定为什么 2 个数组的长度相等:

> pred_svm <- prediction(pred_svm_2class[,2], as.numeric(as.character(y)))
Error in prediction(pred_svm_2class[, 2], as.numeric(as.character(y))) : 
Number of predictions in each run must be equal to the number of labels for each run.

我有下面的代码,输入在这里 click me。这是一个二进制 class化的小型数据集,因此代码运行速度很快。

library("caret")
library("ROCR")
sensor6data_2class <- read.csv("/home/sensei/clustering/svm_2labels.csv")
sensor6data_2class <- within(sensor6data_2class, Class <- as.factor(Class))

set.seed("1298356")
inTrain_svm_2class <- createDataPartition(y = sensor6data_2class$Class, p = .75, list = FALSE)
training_svm_2class <- sensor6data_2class[inTrain_svm_2class,]
testing_svm_2class <- sensor6data_2class[-inTrain_svm_2class,]
trainX <- training_svm_2class[,1:20]
y <- training_svm_2class[,21]

ctrl_svm_2class <- rfeControl(functions = rfFuncs , method = "repeatedcv", number = 5, repeats = 2, allowParallel = TRUE)
model_train_svm_2class <- rfe(x = trainX, y = y, data = training_svm_2class, sizes = c(1:20), metric = "Accuracy", rfeControl = ctrl_svm_2class, method="svmRadial")

pred_svm_2class = predict(model_train_svm_2class, newdata=testing_svm_2class)
pred_svm <- prediction(pred_svm_2class[,2], y)

感谢并感谢您的帮助。

这是因为在行

pred_svm <- prediction(pred_svm_2class[,2], y)

pred_svm_2class[2] 是对测试数据的预测,y 是训练数据的标签。只需像这样在单独的变量中生成用于测试的标签

y_test <- testing_svm_2class[,21]

现在如果你这样做

pred_svm <- prediction(pred_svm_2class[,2], y_test)

不会有错误。完整代码如下 -

# install.packages("caret")
# install.packages("ROCR")
# install.packages("e1071")
# install.packages("randomForest")
library("caret")
library("ROCR")
sensor6data_2class <- read.csv("svm_2labels.csv")
sensor6data_2class <- within(sensor6data_2class, Class <- as.factor(Class))

set.seed("1298356")
inTrain_svm_2class <- createDataPartition(y = sensor6data_2class$Class, p = .75, list = FALSE)
training_svm_2class <- sensor6data_2class[inTrain_svm_2class,]
testing_svm_2class <- sensor6data_2class[-inTrain_svm_2class,]
trainX <- training_svm_2class[,1:20]
y <- training_svm_2class[,21]
y_test <- testing_svm_2class[,21]

ctrl_svm_2class <- rfeControl(functions = rfFuncs , method = "repeatedcv", number = 5, repeats = 2, allowParallel = TRUE)
model_train_svm_2class <- rfe(x = trainX, y = y, data = training_svm_2class, sizes = c(1:20), metric = "Accuracy", rfeControl = ctrl_svm_2class, method="svmRadial")

pred_svm_2class = predict(model_train_svm_2class, newdata=testing_svm_2class)
pred_svm <- prediction(pred_svm_2class[,2], y_test)