【问题标题】:Genetic algorithm using feature selection使用特征选择的遗传算法
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 2011-12-21
    • 2023-03-07
    • 2016-07-18
    • 2019-11-11
    • 2019-06-12
    • 1970-01-01
    相关资源
    最近更新 更多