【发布时间】:2017-03-17 22:36:04
【问题描述】:
我正在尝试通过遗传算法使用插入符号特征选择,但收到一条错误消息。我的代码如下所示:
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
【问题讨论】:
-
您的
y向量的大小似乎有误。也许您正在采样 X,却忘了也采样 Y。 -
亲爱的 Rania,请在格式化您的问题时付出一些努力。要解决您的问题:@Fernando 是对的,您正在使用完整的 y 其中 x 仅使用索引进行训练。错误就在于此。
标签: r genetic-algorithm feature-selection