GASearchCV

class sklearn_genetic.GASearchCV(estimator, cv=3, param_grid=None, scoring=None, population_size=10, generations=40, crossover_probability=0.8, mutation_probability=0.1, tournament_size=3, elitism=True, verbose=True, keep_top_k=1, criteria='max', algorithm='eaMuPlusLambda', refit=True, n_jobs=1, pre_dispatch='2*n_jobs', error_score=nan)[source]

Evolutionary optimization over hyperparameters.

GASearchCV implements a “fit” and a “score” method. It also implements “predict”, “predict_proba”, “decision_function”, “predict_log_proba” if they are implemented in the estimator used. The parameters of the estimator used to apply these methods are optimized by cross-validated search over parameter settings.

Parameters
estimatorestimator object, default=None

estimator object implementing ‘fit’ The object to use to fit the data.

cvint, cross-validation generator or an iterable, default=None

Determines the cross-validation splitting strategy. Possible inputs for cv are: - None, to use the default 5-fold cross validation, - int, to specify the number of folds in a (Stratified)KFold, - CV splitter, - An iterable yielding (train, test) splits as arrays of indices. For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used. These splitters are instantiated with shuffle=False so the splits will be the same across calls.

param_griddict, default=None

Grid with the parameters to tune, expects keys a valid name of hyperparameter based on the estimator selected and as values one of Integer , Categorical Continuous classes

population_sizeint, default=10

Size of the initial population to sample randomly generated individuals.

generationsint, default=40

Number of generations or iterations to run the evolutionary algorithm.

crossover_probabilityfloat, default=0.8

Probability of crossover operation between two individuals.

mutation_probabilityfloat, default=0.1

Probability of child mutation.

tournament_sizeint, default=3

Number of individuals to perform tournament selection.

elitismbool, default=True

If True takes the tournament_size best solution to the next generation.

scoringstr or callable, default=None

A str (see model evaluation documentation) or a scorer callable object / function with signature scorer(estimator, X, y) which should return only a single value.

n_jobsint, default=None

Number of jobs to run in parallel. Training the estimator and computing the score are parallelized over the cross-validation splits. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors.

verbosebool, default=True

If True, shows the metrics on the optimization routine.

keep_top_kint, default=1

Number of best solutions to keep in the hof object. If a callback stops the algorithm before k iterations, it will return only one set of parameters per iteration.

criteria{‘max’, ‘min’} , default=’max’

max if a higher scoring metric is better, min otherwise.

algorithm{‘eaMuPlusLambda’, ‘eaMuCommaLambda’, ‘eaSimple’}, default=’eaMuPlusLambda’

Evolutionary algorithm to use. See more details in the deap algorithms documentation.

refitbool, default=True

Refit an estimator using the best found parameters on the whole dataset. If False, it is not possible to make predictions using this GASearchCV instance after fitting.

pre_dispatchint or str, default=’2*n_jobs’

Controls the number of jobs that get dispatched during parallel execution. Reducing this number can be useful to avoid an explosion of memory consumption when more jobs get dispatched than CPUs can process. This parameter can be:

  • None, in which case all the jobs are immediately created and spawned. Use this for lightweight and fast-running jobs, to avoid delays due to on-demand spawning of the jobs

  • An int, giving the exact number of total jobs that are spawned

  • A str, giving an expression as a function of n_jobs, as in ‘2*n_jobs’

error_score‘raise’ or numeric, default=np.nan

Value to assign to the score if an error occurs in estimator fitting. If set to 'raise', the error is raised. If a numeric value is given, FitFailedWarning is raised.

Attributes
logbookDEAP.tools.Logbook

Contains the logs of every set of hyperparameters fitted with its average scoring metric.

historydict

Dictionary of the form: {“gen”: [], “fitness”: [], “fitness_std”: [], “fitness_max”: [], “fitness_min”: []}

gen returns the index of the evaluated generations. Each entry on the others lists, represent the average metric in each generation.

best_estimator_estimator

Estimator that was chosen by the search, i.e. estimator which gave highest score on the left out data. Not available if refit=False.

best_params_dict

Parameter setting that gave the best results on the hold out data.

decision_function(X)[source]

Call decision_function on the estimator with the best found parameters.

Parameters
Xindexable, length n_samples

Must fulfill the input assumptions of the underlying estimator.

fit(X, y, callbacks=None)[source]

Main method of GASearchCV, starts the optimization procedure with the hyperparameters of the given estimator

Parameters
Xarray-like of shape (n_samples, n_features)

The data to fit. Can be for example a list, or an array.

yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

The target variable to try to predict in the case of supervised learning.

callbacks: list or callable

One or a list of the callbacks methods available in callbacks. The callback is evaluated after fitting the estimators from the generation 1.

predict(X)[source]

Call predict on the estimator with the best found parameters.

Only available if refit=True and the underlying estimator supports predict.

Parameters
X: indexable, length n_samples

Must fulfill the input assumptions of the underlying estimator.

predict_log_proba(X)[source]

Call predict_log_proba on the estimator with the best found parameters.

Only available if refit=True and the underlying estimator supports predict.

Parameters
X: indexable, length n_samples

Must fulfill the input assumptions of the underlying estimator.

predict_proba(X)[source]

Call predict_proba on the estimator with the best found parameters.

Only available if refit=True and the underlying estimator supports predict.

Parameters
X: indexable, length n_samples

Must fulfill the input assumptions of the underlying estimator.

score(X, y=None)[source]

Returns the score on the given data, if the estimator has been refit. This uses the score defined by scoring where provided

Xarray-like of shape (n_samples, n_features)

Input data, where n_samples is the number of samples and n_features is the number of features.

yarray-like of shape (n_samples, n_output) or (n_samples,), default=None

Target relative to X for classification or regression; None for unsupervised learning.