mql4beginner
mql4beginner

Reputation: 2233

How to use genetic algorithm for prediction correctly

I'm trying to use genetic algorithm for classification problem. However, I didn't succeed to get a summary for the model nor a prediction for a new data frame. How can I get the summary and the prediction for the new dataset? Here is my toy example:

library(genalg)
dat <- read.table(text = " cats birds    wolfs     snakes
                  0        3        9         7
                  1        3        8         7
                  1        1        2         3
                  0        1        2         3
                  0        1        2         3
                  1        6        1         1
                  0        6        1         1
                  1        6        1         1   ", header = TRUE) 
evalFunc <- function(x) {
        if (dat$cats < 1) 
        return(0) else return(1)
}
iter = 100
GAmodel <- rbga.bin(size = 7, popSize = 200, iters = iter, mutationChance = 0.01, 
                    elitism = T, evalFunc = evalFunc)

###########summary try#############

cat(summary.rbga(GAmodel))
# Error in cat(summary.rbga(GAmodel)) : 
#   could not find function "summary.rbga"

############# prediction try###########

dat$pred<-predict(GAmodel,newdata=dat)
# Error in UseMethod("predict") : 
#   no applicable method for 'predict' applied to an object of class "rbga"

Update: After reading the answer given and reading this link: Pattern prediction using Genetic Algorithm I wonder how can I programmatically use the GA as part of a prediction mechanism? According to the link's text, one can use the GA for optimizing regression or NN and then use the predict function provided by them/

Upvotes: 8

Views: 2323

Answers (1)

G5W
G5W

Reputation: 37641

Genetic Algorithms are for optimization, not for classification. Therefore, there is no prediction method. Your summary statement was close to working.

cat(summary(GAmodel))
GA Settings
  Type                  = binary chromosome
  Population size       = 200
  Number of Generations = 100
  Elitism               = TRUE
  Mutation Chance       = 0.01

Search Domain
  Var 1 = [,]
  Var 0 = [,]

GA Results
  Best Solution : 1 1 0 0 0 0 1 

Some additional information is available from Imperial College London

Update in response to updated question:

I see from the paper that you mentioned how this makes sense. The idea is to use the genetic algorithm to optimize the weights for a neural network, then use the neural network for classification. This would be a big task, too big to respond here.

Upvotes: 4

Related Questions