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):
#ToDo: Check that the columns or the feature names are not same
#ToDo: All other general sanity checks are also to be made.
#ToDo: make all the parameters in the init function
input_feat = list(X.columns.values);
creator.create("FitnessMax", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
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)
pop = toolbox.population(n=self.N_population)
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)
X = (X - self._X_mean)/self._X_std
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=self.test_size)
df_train = pd.concat([pd.DataFrame(X_train), y_train], axis = 1)
# Setting up the global test (because this is Fitness Ensemble Global Test)
df_test = pd.concat([X_test, y_test], axis = 1)
### Setting toolbox
creator.create("FitnessMax", base.Fitness, weights=(-1.0,)) #lower the error the better
creator.create("Individual", list , fitness=creator.FitnessMax) #list of Estimator Genes
toolbox = base.Toolbox()
## In this case we will also need to pass the base estimator.
toolbox.register("mdl_sample", get_mdl_sample, self.init_sample_percentage, df_train, self.base_estimator)
## Initially, the Gene was a dataframe and not trained model. But in DEAP evaluations on individuals
## are saved and not done again if the fitness remains unchanged. That would help avoid re-training
## of all models in the Individual. Hence, Gene is a custom class EstimatorGene.
# n defines the size of the ensemble.
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.mdl_sample, self.n)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
# evalOneMax_KNN_EG is where the FEGT logic is saved.
toolbox.register("evaluate", evalOneMax_KNN_EG, df_test = df_test, base_estimator = self.base_estimator, n_votes = self.n_votes)
def deap_test():
# onemax example evolves to print list of ones: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
numpy.random.seed(1)
def evalOneMax(individual):
return sum(individual),
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, typecode='b', fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("attr_bool", numpy.random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 10)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", evalOneMax)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
pop = toolbox.population(n=50)
hof = 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)
X, y = indexable(X, y)
if y is not None:
if len(y) != n_samples:
raise ValueError(
"Target variable (y) has a different number "
"of samples (%i) than data (X: %i samples)" % (len(y), n_samples)
)
cv = self.cv
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RuntimeWarning)
# * y.shape[1]
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create(
"Individual", list, est=clone(self.estimator), fitness=creator.FitnessMax
)
toolbox = base.Toolbox()
name_values, gene_type, maxints = _get_param_types_maxint(parameter_dict)
if self.gene_type is None:
self.gene_type = gene_type
if self.verbose:
print("Types %s and maxint %s detected" % (self.gene_type, maxints))
toolbox.register("individual", _initIndividual, creator.Individual, maxints=maxints)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register(
"evaluate",
#
# You should have received a copy of the GNU Lesser General Public
# License along with DEAP. If not, see .
import array
import random
import numpy
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
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)
def __init__(self, lines, pid,):
self.toolbox = base.Toolbox()
self.lc, self.lb, self.li = lines
self.puzzle_id = 0
self.pid = pid
creator.create("FitnessMax", base.Fitness, weights=(-1.0,))
creator.create("Puzzle", Puzzle, fitness=creator.FitnessMax)
self.toolbox.register("puzzle", creator.Puzzle, self.get_args_lines_index)
self.toolbox.register("population", tools.initRepeat, list, self.toolbox.puzzle)
# toolbox.register("mate", tools.cxTwoPoint)
# toolbox.register("select", tools.selTournament, tournsize=3)
self.toolbox.register("evaluate", lambda pop, eval: pop.evaluate(eval=eval))
#
self.stats = tools.Statistics(key=lambda ind: ind.fitness.values)
# self.stats.register("min", min)
# self.stats.register("max", max)
self.logbook = tools.Logbook()
self.logbook.header = "eval", "population", "fitnesses"
maxsize = 10
self.famous = HallOfFame(maxsize)
"""The same One Max example similar to the short version but using
:class:`numpy.ndarray` as base class.
**Keywords:** bit string, minimization, :class:`numpy.ndarray`
"""
import numpy
import random
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", numpy.ndarray, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("attr_bool", random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=100)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def evalOneMax(individual):
return sum(individual),
toolbox.register("evaluate", evalOneMax)
toolbox.register("mate", tools.cxTwoPoints)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
def main():
bondrots2 = []
for b in chimera.selection.savedSels['bonds2'].bonds():
br = chimera.BondRot(b)
br.myanchor = gaudi.utils.box.find_nearest(anchor2, b.atoms)
bondrots2.append(br)
mol = chimera.openModels.list()[0]
ligands = chimera.selection.savedSels['ligands'].residues()
ligand_atoms = [ a for r in ligands for a in r.atoms ]
###
# Genetic Algorithm
# define individual, population, etc
deap.creator.create("FitnessMax", deap.base.Fitness, weights=(-1.0, 1.0, 1.0))
deap.creator.create("Individual", list, fitness=deap.creator.FitnessMax)
toolbox = deap.base.Toolbox()
toolbox.register("rand_angle", random.uniform, 0, 360)
toolbox.register("individual", deap.tools.initRepeat, deap.creator.Individual,
toolbox.rand_angle, n=len(bondrots1)+len(bondrots2))
toolbox.register("population", deap.tools.initRepeat,
list, toolbox.individual)
toolbox.register("evaluate", evalCoord)
toolbox.register("mate", deap.tools.cxSimulatedBinaryBounded,
eta=10., low=0., up=360.)
toolbox.register("mutate", deap.tools.mutPolynomialBounded,
eta=1., low=0., up=360., indpb=0.05)
toolbox.register("select", deap.tools.selNSGA2)
def main():
def GA_setup(guess, weights=(1.0,)):
creator.create("FitnessMax", base.Fitness, weights=weights)
creator.create("Individual", array.array, typecode='i',
fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("individual", creator.Individual, guess)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mate", tools.cxPartialyMatched)
toolbox.register("mutate", genome_mutation)
toolbox.register("select", tools.selTournament, tournsize=3)
return toolbox