2017-03-17 33 views
0

我想使用遺傳算法使用插入特徵選擇,得到錯誤消息。我的代碼如下所示:使用特徵選擇的遺傳算法

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 
+0

它看起來像你的'y'矢量有錯誤的大小。也許你正在抽樣X,忘了也抽樣Y. – Fernando

+0

親愛的拉尼婭,請在格式化你的問題時表現出一些努力。爲了解決你的問題:@Fernando是對的,你正在使用完整的y,因爲x只使用指數進行訓練。這就是錯誤。 – Drey

回答

1

這是一個常見的錯誤。您採樣X,但忘了品嚐Y.這樣做:

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