0

我正在尝试将 rfeControl 和 rfe 用于使用 svm 的简单特征选择任务。输入文件很小,有 20 个特征和 414 个样本。输入可以在这里找到 [ https://www.dropbox.com/sh/hj91gd06dbbyi1o/AABTHPuP4kI85onSqBiGH_ISa?dl=0]

忽略警告,我对以下错误的不理解是,据我了解,当 metric==RMSE 时最大化取值,但是,我在执行分类时具有 metric==Accuracy(参考:https ://github.com /topepo/caret/blob/master/pkg/caret/R/rfe.R):

Error in if (maximize) which.max(x[, metric]) else which.min(x[, metric]) : 
argument is not interpretable as logical
In addition: Warning message:
In if (maximize) which.max(x[, metric]) else which.min(x[, metric]) :
the condition has length > 1 and only the first element will be used

代码如下:

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

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

ctrl <- rfeControl(functions = rfFuncs , method = "repeatedcv", number = 5, repeats = 2, allowParallel = TRUE)
model_train <- rfe(x = trainX, y = y, sizes = c(10,11), metric = "Accuracy" , Class2 ~ ZCR + Energy + SpectralC + SpectralS + SpectralE + SpectralF + SpectralR + MFCC1 + MFCC2 + MFCC3 + MFCC4 + MFCC5 + MFCC6 + MFCC7 + MFCC8 + MFCC9 + MFCC10 + MFCC11 + MFCC12 + MFCC13, rfeControl = ctrl, method="svmRadial")

提前致谢。

4

1 回答 1

1

您的代码中有多个错误。

  1. 您正在创建一个新的 class2,但没有选择它作为 Y,您选择的是 Class
  2. 您在 rfe 和 x 和 y 表示法中使用公式表示法。这会导致您得到的错误。使用 x 和 y 或使用公式表示法。检查下面的示例代码。

下面的代码有效:

library("caret")
sensor6data_2class <- read.csv("svm_2labels.csv")
sensor6data_2class$Class <- as.factor(sensor6data_2class$Class)
# sensor6data_2class$Class2 <- relevel(sensor6data_2class$Class,ref="1")

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

ctrl <- rfeControl(functions = rfFuncs , 
                   method = "repeatedcv",
                   number = 5, 
                   repeats = 2, 
                   allowParallel = TRUE)
set.seed("1298356")
model_train <- rfe(x = trainX, 
                   y = y, 
                   sizes = c(10,11), 
                   metric = "Accuracy" , 
                   rfeControl = ctrl)
set.seed("1298356")
model_train_form <- rfe(Class ~ ZCR + Energy + SpectralC + SpectralS + SpectralE + SpectralF + SpectralR + MFCC1 + MFCC2 + MFCC3 + MFCC4 + MFCC5 + MFCC6 + MFCC7 + MFCC8 + MFCC9 + MFCC10 + MFCC11 + MFCC12 + MFCC13, 
                        data = training,
                        sizes = c(10,11), 
                        metric = "Accuracy",
                        rfeControl = ctrl)
于 2016-11-11T09:00:46.683 回答