API Reference / automl /

search_space.py

Turn a component's JSON Schema into a searchable parameter space.

Every TuiML algorithm already publishes get_parameter_schema, a JSON Schema per constructor parameter. This module reads that schema and derives a ParameterDistribution from it, so a tuner can search any algorithm in the library without a hand-written grid.

The module is deliberately standalone: it depends only on the registry metadata and tuning, so RandomSearchCV and friends can use it directly, with or without automl.

python
>>> from tuiml.automl.search_space import search_space_for
>>> from tuiml.algorithms.trees import DecisionTreeClassifier
>>> space = search_space_for(DecisionTreeClassifier)
>>> sorted(space.param_distributions)
['criterion', 'max_depth', 'min_samples_leaf', 'min_samples_split']
>>> space.skipped_["random_state"]
'excluded: not a model-behaviour parameter'

Functions

Func

schema_to_distribution

Line 303
schema_to_distribution(schema: Dict[str, Dict[str, Any]], *, include: Optional[Iterable[str]]=None, exclude: Optional[Iterable[str]]=None) -> Tuple[Dict[str, Any], Dict[str, str]]

Convert a parameter schema into ParameterDistribution specs.

Each parameter is mapped to one of the forms ParameterDistribution understands:

================================== ================================ Schema fragment Distribution spec ================================== ================================ {"enum": [...]} the list of enum values {"type": "boolean"} [True, False] {"type": "integer", min, max} (min, max, 'int') {"type": "number", min, max} (min, max) or (min, max, 'log') {"type": ["integer", "null"]} [None, level, level, ...] {"default": None, min, no max} [None, level, level, ...] ================================== ================================

A union with "null" keeps None as an explicit choice, since for parameters like max_depth "no limit" is a real setting rather than a missing value. A union of two or more concrete types (for example ["number", "string"] for a parameter that also accepts "auto") is skipped unless it carries an enum: there is no safe way to guess which arm the tuner should explore.

Parameters

schema
dict
Mapping of parameter name to its JSON Schema fragment, as returned by get_parameter_schema.
include
iterable of str
If given, only these parameter names are considered.
exclude
iterable of str
Extra parameter names to drop, on top of EXCLUDED_PARAMETERS.

Returns

distributions
dict
Parameter name to distribution spec, ready for ParameterDistribution.
skipped
dict
Parameter name to a human-readable reason it was left out.
python
>>> from tuiml.automl.search_space import schema_to_distribution
>>> schema = {
...     "alpha": {"type": "number", "minimum": 0.0001, "maximum": 1.0},
...     "fit_intercept": {"type": "boolean", "default": True},
...     "n_jobs": {"type": "integer", "default": 1},
... }
>>> dists, skipped = schema_to_distribution(schema)
>>> dists["alpha"]
(0.0001, 1.0, 'log')
>>> dists["fit_intercept"]
[True, False]
>>> skipped["n_jobs"]
'excluded: not a model-behaviour parameter'
Func

search_space_for

Line 452
search_space_for(algorithm_cls, *, include: Optional[Iterable[str]]=None, exclude: Optional[Iterable[str]]=None) -> ParameterDistribution

Build a searchable parameter space for one algorithm class.

Reads the class's get_parameter_schema, converts it with schema_to_distribution, and drops anything the constructor does not actually accept -- a schema occasionally documents a parameter that the current signature no longer takes, and sampling it would raise.

Parameters

algorithm_cls
type or str
The algorithm class, or its registry name ("RandomForestClassifier").
include
iterable of str
If given, only these parameter names are searched.
exclude
iterable of str
Extra parameter names to drop, on top of EXCLUDED_PARAMETERS.

Returns

space
ParameterDistribution
The search space. The attribute space.skipped_ maps every parameter that was left out to the reason why, so an empty or surprising space can be debugged without re-deriving it.
python
>>> from tuiml.automl.search_space import search_space_for
>>> space = search_space_for("RandomForestClassifier")
>>> space.param_distributions["criterion"]
['gini', 'entropy']
>>> params = space.sample(random_state=0)
>>> sorted(params)
['bootstrap', 'criterion', 'max_depth', 'min_samples_leaf', 'min_samples_split', 'n_estimators', 'oob_score']
>>> space.skipped_["max_features"]
"ambiguous union type ['string', 'integer', 'number'] without an enum; no safe way to pick which arm to search"