遗传算法使用特征选择

时间:2017-03-17 14:05:39

标签: r genetic-algorithm feature-selection

我正在尝试使用遗传算法进行插入特征选择,我收到一条错误消息。我的代码如下所示:

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

1 个答案:

答案 0 :(得分:1)

这是一个常见的错误。您对X进行了采样,但忘了对Y进行采样。请执行以下操作:

ytrain = y[trainIndex]
ytest = y[-trainIndex]