2016-11-10 54 views
0

我正嘗試使用rfeControl和rfe進行簡單的使用svm的功能選擇任務。輸入文件很小,有20個特徵,414個樣本。輸入可以在這裏找到[https://www.dropbox.com/sh/hj91gd06dbbyi1o/AABTHPuP4kI85onSqBiGH_ISa?dl=0]R符號包rfe錯誤 - 「參數不可理解爲邏輯」

忽略警告,我不明白下面的錯誤是,因爲我明白當衡量指標== RMSE和我時,最大化的價值,但是,具有指標==準確性,因爲我正在執行分類(參考: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") 

感謝。

回答

1

您的代碼有多個錯誤。

  1. 您正在創建一個新的Class2,但不選擇它作爲Y,你選擇
  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) 
+0

感謝@phiver。也爲我工作得很好!如果我可能會問一個後續問題,我試圖用預測函數預測輸出,使用測試數據,並且不輸出所有樣本的預測結果。任何想法爲什麼?我有代碼和輸出在跟進答案。謝謝! – tacqy2