Search Space
The sklearn_genetic.space module provides three dimension types for defining the hyperparameter search space. All three accept a random_state for direct sampling. Pass an integer for a repeatable sequence, or pass a NumPy RandomState or Generator to advance a shared random stream.
Integer
Samples integer values from a range [lower, upper].
from sklearn_genetic.space import Integer
Integer(lower, upper, distribution="uniform")| Parameter | Type | Default | Description |
|---|---|---|---|
lower | int | — | Minimum value (inclusive) |
upper | int | — | Maximum value (inclusive) |
distribution | str | "uniform" | Sampling distribution. Use "log-uniform" to sample across orders of magnitude. |
Candidate repair and local refinement preserve integer values exactly, including adjacent values above 2**53. Pass large integer warm-start values as Python or NumPy integers to preserve their identity. Equal bounds hold an integer parameter fixed. Sampling that dimension returns the exact Python integer, including values outside the signed 64-bit range. Log-uniform bounds must be positive. For variable bounds, log-uniform sampling draws a value on [lower, upper + 1) and rounds down. Each integer k has probability log((k + 1) / k) / log((upper + 1) / lower). The initializer and built-in optimizer backends use the same bins. DEAP local refinement measures its radius in log coordinates and skips the current bin. If the radius covers only that bin, refinement tries an adjacent in-bounds integer.
The precision check for very wide ranges requires (upper + 1) * log((upper + 1) / lower) <= 2**48 for variable bounds. Narrow ranges can use large integer endpoints because the calculation uses offsets from lower. If the logarithmic span is below float precision, the bins have equal width at that precision.
To evaluate a few scales early while keeping a full integer range, see the integer warm-start example.
Example:
from sklearn_genetic.space import Integer
param_grid = {
"n_estimators": Integer(16, 1024, distribution="log-uniform"),
"max_depth": Integer(1, 20),
}Continuous
Samples floating-point values from a range [lower, upper].
from sklearn_genetic.space import Continuous
Continuous(lower, upper, distribution="uniform")| Parameter | Type | Default | Description |
|---|---|---|---|
lower | float | — | Minimum value |
upper | float | — | Maximum value |
distribution | str | "uniform" | Sampling distribution: "uniform" or "log-uniform" |
Equal bounds hold a continuous parameter fixed. For example, Continuous(0.01, 0.01, distribution="log-uniform") always proposes 0.01 while other dimensions evolve. Bounds must be finite and within the float range. A fixed value must be exactly representable as a float. A variable interval must contain at least one representable float and uniform sampling requires a finite span. Use Integer when you need to sample exact integers beyond float precision. Log-uniform bounds must both be positive. If distinct bounds round to the same logarithm, sampling uses the interval in the original scale at float precision.
Use distribution="log-uniform" for parameters that span orders of magnitude (learning rates, regularization strengths):
"learning_rate": Continuous(1e-4, 1e-1, distribution="log-uniform"),
"alpha": Continuous(1e-6, 1.0, distribution="log-uniform"),Categorical
Values that compare equal in Python can configure an estimator differently. For example, Categorical([1, 1.0]) keeps two distinct per-split feature limits for a decision tree's max_features. The integer uses one feature and the float uses the full feature count. The cache, coverage count and finalist shortlist use the declared types to distinguish them. When an older checkpoint may have merged their scores, start the search with a new checkpoint file.
Samples from a fixed list of choices.
from sklearn_genetic.space import Categorical
Categorical(choices, priors=None, distribution="choice", random_state=None)| Parameter | Type | Default | Description |
|---|---|---|---|
choices | list | — | List of valid values. Can include None |
priors | list of floats | None | Sampling probabilities in the same order as choices. They must be nonnegative and sum to one. |
distribution | str | "choice" | Distribution used to sample this dimension. |
random_state | int, RandomState, Generator or None | None | Seed or shared random stream for direct sampling. |
For example, Categorical(["gini", "entropy"], priors=[0.75, 0.25]) assigns three quarters of the sampling probability to Gini. Smart initialization stratifies the weighted draws across the available population slots. With four slots this allocation gives three Gini draws and one entropy draw before duplicate removal. With fewer slots, the search seed controls which categories receive the fractional slots. Warm starts and estimator defaults are added separately.
A zero weight excludes a choice when the initializer samples this dimension. For example, Categorical(["gini", "entropy", "log_loss"], priors=[1, 0, 0]) samples only "gini". A warm start such as {"criterion": "entropy"} still puts "entropy" in the initial population. An eligible estimator default can also supply a zero-weight choice. Once the smart initializer has seen every combination it can draw, it fills the remaining slots with ordinary draws. The search still counts all declared choices when reporting full-space coverage.
When all positive weights are equal, the stratified draws use the same seeded sample as priors=None over the eligible choices. If fewer slots remain than eligible choices, any subset of the requested size can be drawn.
Example:
"max_features": Categorical(["sqrt", "log2", None]),
"solver": Categorical(["lbfgs", "sgd", "adam"]),
"activation": Categorical(["relu", "tanh", "logistic"]),For preconfigured estimator choices, the search matches a copied estimator to a choice by its constructor parameters. An exact match in values and types takes priority over a choice whose values merely compare equal. Object arrays in constructor parameters compare by shape, dtype and cell contents, including nested arrays and dictionaries. For example, an integer 1 in an object-array cell prefers the declared integer choice when a Boolean True choice also compares equal.
If a pipeline step accepts missing-value markers, you can include them in its search space. For a step named imputer that uses SimpleImputer with numeric data, you can use the following dimension.
import numpy as np
import pandas as pd
from sklearn_genetic.space import Categorical
param_grid = {"imputer__missing_values": Categorical([np.nan, pd.NA])}Both markers are separate choices and can use the cache. With markers whose equality is ambiguous or non-reflexive, the search skips the early stopping check for full finite-space coverage. Other stopping conditions still apply.
For a floating-point NaN listed directly in choices, warm starts and checkpoints match the declared type and bit pattern. This applies to Python float and NumPy float16, float32, and float64. Use the same scalar type in warm_start_configs as in choices.
Complete Example
from sklearn_genetic.space import Categorical, Continuous, Integer
param_grid = {
"n_estimators": Integer(50, 300),
"max_depth": Integer(2, 15),
"learning_rate": Continuous(0.01, 0.3, distribution="log-uniform"),
"subsample": Continuous(0.5, 1.0),
"max_features": Categorical(["sqrt", "log2"]),
"min_samples_leaf": Integer(1, 20),
}Convert sklearn/scipy-style spaces
from_sklearn_space converts common RandomizedSearchCV-style dictionaries into native sklearn-genetic-opt dimensions. Use it when migrating an existing param_distributions mapping; when defining a new space, prefer the native dimensions directly:
from scipy import stats
from sklearn_genetic.space import from_sklearn_space
param_grid = from_sklearn_space({
"n_estimators": stats.randint(50, 300),
"learning_rate": stats.loguniform(1e-3, 1e-1),
"max_features": stats.uniform(0.2, 0.8),
"criterion": ["gini", "entropy"],
})Conversion rules:
| Input value | Output dimension |
|---|---|
list, tuple, set, range, numpy array | Categorical([...]) |
scipy.stats.randint(low, high, loc=0) | Integer(low + loc, high - 1 + loc) for an integer offset |
scipy.stats.uniform(loc, scale) | Continuous(loc, loc + scale) |
scipy.stats.loguniform(a, b, scale=s) / reciprocal(a, b, scale=s) | Continuous(a * s, b * s, distribution="log-uniform") for zero loc (default scale is 1) |
Existing Integer, Continuous, Categorical | returned unchanged |
A fractional offset in randint produces noninteger support. A nonzero offset in loguniform changes the probability law even if both endpoints stay positive. The converter raises a ValueError for either case. To supply selected values from such a distribution, pass them as a list. Use an ordered list or tuple for reproducible categorical sampling across processes. A set has no fixed order.
Unsupported scipy distributions raise an actionable ValueError that names the distribution and suggests defining the corresponding Integer, Continuous, or Categorical dimension manually.
See Also
- Migrating from RandomizedSearchCV — copyable conversion recipe and bound semantics
- Basic Usage — tutorial using all three dimension types
- Presets — starter spaces for common scikit-learn estimators
- GASearchCV — the search estimator that consumes
param_grid