Skip to content

GASearchCV

Genetic algorithm hyperparameter search for scikit-learn estimators.

GASearchCV implements the scikit-learn estimator interface and follows the same patterns as GridSearchCV and RandomizedSearchCV. After calling fit, it exposes best_params_, best_score_, cv_results_, predict, predict_proba, and score.

Class Signature

python
from sklearn_genetic import GASearchCV

GASearchCV(
    estimator,
    param_grid,
    *,
    scoring=None,
    cv=5,
    refit=True,
    verbose=0,
    error_score=np.nan,
    return_train_score=False,
    evolution_config=None,
    population_config=None,
    runtime_config=None,
    optimization_config=None,
)

Parameters

ParameterTypeDefaultDescription
estimatorestimatorA scikit-learn estimator with a fit method
param_griddictMapping from parameter name to Integer, Continuous, or Categorical
scoringstr or dictNoneMetric(s) to evaluate. None uses the estimator's default scorer
cvint or CV splitter5Cross-validation strategy
refitbool or strTrueMetric to use when refitting the best model. If scoring is a dict, pass the metric name
verboseint0Verbosity level. 1 = generation log
error_scorefloat or "raise"np.nanScore to use when a candidate raises an exception
return_train_scoreboolFalseInclude training scores in cv_results_
evolution_configEvolutionConfigNoneControls population size, generations, crossover/mutation rates, elitism
population_configPopulationConfigNoneControls initialization strategy, warm starts, diversity
runtime_configRuntimeConfigNoneControls parallelism, caching, verbosity
optimization_configOptimizationConfigNoneControls local search, fitness sharing

Attributes After fit

AttributeDescription
best_params_Parameter setting that gave the best mean cross-validated score
best_score_Mean cross-validated score of the best estimator
best_estimator_Estimator fitted with best_params_ on the full training data (if refit=True)
cv_results_Dict with per-candidate results, compatible with pd.DataFrame
historyList of per-generation dicts with fitness and diversity telemetry
logbookDEAP logbook — same data as history in DEAP's format
fit_stats_Dict with evaluation counters (cache hits, skipped candidates, etc.)
support_(Not applicable for GASearchCV — see GAFeatureSelectionCV)

Methods

MethodDescription
fit(X, y, callbacks=None)Run the genetic search
predict(X)Predict using best_estimator_
predict_proba(X)Predict class probabilities (if estimator supports it)
score(X, y)Score using best_estimator_
get_params() / set_params()Standard sklearn estimator interface

Example

python
from sklearn_genetic import EvolutionConfig, GASearchCV, PopulationConfig, RuntimeConfig
from sklearn_genetic.space import Categorical, Continuous, Integer

search = GASearchCV(
    estimator=your_estimator,
    param_grid={
        "param1": Integer(1, 100),
        "param2": Continuous(0.01, 1.0, distribution="log-uniform"),
        "param3": Categorical(["a", "b", "c"]),
    },
    cv=5,
    scoring="roc_auc",
    evolution_config=EvolutionConfig(population_size=20, generations=15),
    population_config=PopulationConfig(initializer="smart"),
    runtime_config=RuntimeConfig(n_jobs=-1, use_cache=True),
)

search.fit(X_train, y_train)
print(search.best_params_)
print(search.best_score_)

See Also

Released under the MIT License.