0

I am trying to use Caret Feature Selection using Genetic Algorithms, am getting an error message. My code is shown below :

set.seed(10)
trainIndex <- createDataPartition(iris$Species, p = .5, list = FALSE, times = 1)
trainData <- iris[trainIndex,-c(1,2)]
testData <- iris[-trainIndex,-c(1,2)]
trainX <-trainData[,-1]
testX <- testData[,-1]
y=trainData$Class
data(iris)
dim(iris) 
# [1] 150   5
head(iris,2)   
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa 
# 2          4.9         3.0          1.4         0.2  setosa
registerDoParallel(4)
getDoParWorkers() [1] 4
utils:::menuInstallLocal()
# le package ‘GA’ a été décompressé et les sommes MD5 ont été vérifiées avec succés
ga_ctrl <- gafsControl(functions = rfGA, method = "cv", genParallel=TRUE, allowParallel = TRUE)
set.seed(10)
lev <- c("PS","WS")
system.time(rf_ga3 <- gafs(x = trainX, y = y, iters = 100, popSize = 20, levels = lev, gafsControl = ga_ctrl))
# Erreur dans gafs.default(x = trainX, y = y, iters = 100, popSize = 20, levels = lev,  :    
#    there should be the same number of samples in x and y Timing stopped at: 0 0 0
2
  • It looks like your y vector have the wrong size. Maybe you're sampling X and forgot to also sample Y. Commented Mar 17, 2017 at 14:09
  • Dear Rania, please show some effort in formatting your question. To address your question: @Fernando is right, that your are using complete y where as x only uses indices for training. Therein lies the mistake. Commented Mar 17, 2017 at 14:10

1 Answer 1

1

It's a common mistake. You sampled X but forgot to sample Y. Do something like:

ytrain = y[trainIndex]
ytest = y[-trainIndex]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.