Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def fit(self, X, y):
'''
The function takes as input:
X: Features of the whole dataset
All features which are to be used for building the model.
y: The output which is to be predicted with the model.
The model is trained to predict these models via X.
'''
input_feat = list(X.columns.values);
creator.create("FitnessMax", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
# The train and test sample for the
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=self.test_size)
self.X_train = X_train
self.X_test = X_test
self.y_train = y_train
self.y_test = y_test
toolbox = base.Toolbox()
toolbox.register("attr_bool", self.get_indiv_sample, data=X_train, output=y_train, base_estimator=self.base_estimator)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=self.N_individual)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", evalOneMax,x_te = X_test, y_te = y_test, test_frac = self.test_frac, test_frac_flag = self.test_frac_flag)
toolbox.register("mate", self.crossover_func)
toolbox.register("mutate", mutate_feat, indpb=self.indpb,input_fe = input_feat, X_tr = X_train)
toolbox.register("select", tools.selTournament, tournsize=3)
def get_toolbox(self, predictors, response, pset, variable_type_indices, variable_names):
subset_size = int(math.floor(predictors.shape[0] * self.subset_proportion))
creator.create("ErrorAgeSizeComplexity", base.Fitness, weights=(-1.0, -1.0, -1.0, -1.0))
creator.create("Individual", sp.SimpleParametrizedPrimitiveTree, fitness=creator.ErrorAgeSizeComplexity,
age=int)
toolbox = base.Toolbox()
toolbox.register("expr", sp.generate_parametrized_expression,
partial(gp.genHalfAndHalf, pset=pset, min_=self.min_depth_init, max_=self.max_depth_init),
variable_type_indices, variable_names)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("compile", gp.compile, pset=pset)
toolbox.register("select", tools.selRandom)
toolbox.register("koza_node_selector", operators.internally_biased_node_selector,
bias=self.internal_node_selection_bias)
self.history = tools.History()
toolbox.register("mate", operators.one_point_xover_biased, node_selector=toolbox.koza_node_selector)
toolbox.decorate("mate", operators.static_limit(key=operator.attrgetter("height"), max_value=self.max_height))
toolbox.decorate("mate", operators.static_limit(key=len, max_value=self.max_size))
## 2.1 Fitness
from deap import base
from deap import creator
## FitnessMin
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
## FitnessMulti
creator.create("FitnessMulti", base.Fitness, weights=(-1.0, 1.0))
from collections import deque
import numpy
from deap import algorithms
from deap import base
from deap import benchmarks
from deap import cma
from deap import creator
from deap import tools
# Problem size
N = 30
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
def main(verbose=True):
NRESTARTS = 10 # Initialization + 9 I-POP restarts
SIGMA0 = 2.0 # 1/5th of the domain [-5 5]
toolbox = base.Toolbox()
toolbox.register("evaluate", benchmarks.rastrigin)
halloffame = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", numpy.mean)
stats.register("std", numpy.std)
stats.register("min", numpy.min)
stats.register("max", numpy.max)
def init_fitness_and_inds():
logger.start_timer()
"""Initializes the fitness and definition of individuals"""
class Fitness(base.Fitness):
def __init__(self):
super().__init__()
self.__fitness_score = None
@property
def fitness_score(self):
return self.values[0]
@fitness_score.setter
def fitness_score(self, fitness_score):
self.__fitness_score = fitness_score
if fitness_score:
# WARNING:
# Setting values breaks alot of things:
# self.__fitness_score is reset to None
# after setting values, so you should only
def getGlobalToolbox(representationModule):
# GLOBAL FUNCTION TO GET GLOBAL TBX UNDER DEVELOPMENT;
toolbox = base.Toolbox()
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create(
"Individual",
list,
fitness=creator.FitnessMax,
PromoterMap=None,
Strategy=genconf.Strategy,
)
toolbox.register("mate", representationModule.crossover)
toolbox.register("mutate", representationModule.mutate)
PromoterMap = initPromoterMap(Attributes)
toolbox.register("newind", initInd, creator.Individual, PromoterMap)
toolbox.register("population", tools.initRepeat, list, toolbox.newind)
toolbox.register("constructPhenotype", representationModule.constructPhenotype)
return toolbox
MAX_ITEM = 50
MAX_WEIGHT = 50
NBR_ITEMS = 20
# To assure reproductibility, the RNG seed is set prior to the items
# dict initialization. It is also seeded in main().
random.seed(64)
# Create the item dictionary: item name is an integer, and value is
# a (weight, value) 2-uple.
items = {}
# Create random items and store them in the items' dictionary.
for i in range(NBR_ITEMS):
items[i] = (random.randint(1, 10), random.uniform(0, 100))
creator.create("Fitness", base.Fitness, weights=(-1.0, 1.0))
creator.create("Individual", set, fitness=creator.Fitness)
toolbox = base.Toolbox()
# Attribute generator
toolbox.register("attr_item", random.randrange, NBR_ITEMS)
# Structure initializers
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.attr_item, IND_INIT_SIZE)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def evalKnapsack(individual):
weight = 0.0
value = 0.0
for item in individual:
def pareto_front(weights, objectives, take=None):
'''Pareto argsort of objectives which may be multi-objective
Parameters:
:weights: list of weights, 1 for max, -1 for minimize
:objectives: list of objective scores where each score is a sequence as long as weights
:take: take the top N only
Returns:
:best_idxes: argsort indices for fitness
'''
toolbox = base.Toolbox()
take = take or objectives.shape[0]
creator.create("FitnessMulti", base.Fitness, weights=weights)
creator.create("Individual",
array.array,
typecode='d',
fitness=creator.FitnessMulti)
toolbox.register('evaluate', lambda x: x)
objectives = [creator.Individual(objectives[idx, :])
for idx in range(objectives.shape[0])]
for (idx, obj) in enumerate(objectives):
obj.idx = idx
obj.fitness.values = toolbox.evaluate(obj)
sel = selNSGA2(objectives, take)
return tuple(item.idx for item in sel)
import array
import random
import numpy
from functools import partial
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
from scoop import futures
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", array.array, typecode='b', fitness=creator.FitnessMax)
toolbox = base.Toolbox()
# Attribute generator
toolbox.register("attr_bool", random.randint, 0, 1)
# Structure initializers
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 100)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def evalOneMax(individual):
return sum(individual),
toolbox.register("evaluate", evalOneMax)
toolbox.register("mate", tools.cxTwoPoint)
from deap import algorithms
from deap import base
from deap import benchmarks
from deap.benchmarks.tools import diversity, convergence
from deap import creator
from deap import tools
from Population import *
from network import Neterr
from chromosome import Chromosome
n_hidden = 10
indim = 8
outdim = 1
creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0, -1.0, -1.0))
creator.create("Individual", Chromosome, fitness=creator.FitnessMin)
toolbox = base.Toolbox()
def minimize(individual):
network_obj = Neterr(indim, outdim, n_hidden, np.random)
outputarr = network_obj.feedforward_ne(individual)
neg_log_likelihood_val = give_neg_log_likelihood(outputarr, network_obj.resty)
mean_square_error_val = give_mse(outputarr, network_obj.resty)
false_positve_rat = give_false_positive_ratio(outputarr, network_obj.resty)
false_negative_rat = give_false_negative_ratio(outputarr, network_obj.resty)
return neg_log_likelihood_val, mean_square_error_val, false_positve_rat, false_negative_rat
def mycross(ind1, ind2):