Skip to content

Instantly share code, notes, and snippets.

@markcheno
Forked from cmd-ntrf/deap_reg_12args.py
Created July 30, 2014 21:06
Show Gist options
  • Save markcheno/ddc11e4364feb51604c5 to your computer and use it in GitHub Desktop.
Save markcheno/ddc11e4364feb51604c5 to your computer and use it in GitHub Desktop.
import operator
import numpy
from math import sqrt
from deap import gp
# Our dataset is 100 random arrays of 0s and 1s
# and the function we are trying to find is simply
# the sum of every arguments ARG0+ARG1+...+ARG11
dataset = numpy.random.random_integers(0, 1, (100, 12))
targets = numpy.sum(dataset, axis=1)
# Initialization of a trivial primitive set
pset = gp.PrimitiveSet("MAIN", 12)
pset.addPrimitive(operator.add, 2)
pset.addTerminal(1.0)
def evaluate(individual):
func = gp.lambdify(individual, pset)
sum_ = 0
# Compute the output for each input then
# sum to the rest the squared difference
# between the target and the actual output.
for data, target in zip(dataset, targets):
# The star (*) unpack the list of 12 elements
# to provide the 12 argument to the function.
output = func(*data)
sum_ += (target - output)**2
rmse = sqrt(sum_ / len(outputs))
# DEAP requires that evaluation function to always
# return a tuple
return rmse,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment