gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:19:54 24 4
gpt4 key购买 nike

我在 svm 中使用了“rfe”函数来创建一个特征减少的模型。然后我对输出类标签(二进制)、0 类概率、1 类概率的测试数据使用“预测”。然后我尝试在 ROCR 包中使用预测函数预测概率和真实类标签,但出现以下错误,我不确定为什么这两个数组的长度相等:

> 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 .它是一个二进制分类的小型数据集,因此代码运行速度很快。

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)

关于r - 预测错误 - ROCR 包(使用概率),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40856895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com