sklab.search¶
sklab.search.GridSearchConfig
dataclass
¶
Quick config for sklearn GridSearchCV.
Source code in src/sklab/_search/sklearn.py
create_searcher(*, pipeline, scoring, cv, n_trials, timeout)
¶
Source code in src/sklab/_search/sklearn.py
__init__(param_grid, scoring=None, cv=None, refit=True, n_jobs=None, verbose=0, pre_dispatch='2*n_jobs', error_score='raise')
¶
sklab.search.RandomSearchConfig
dataclass
¶
Quick config for sklearn RandomizedSearchCV.
Source code in src/sklab/_search/sklearn.py
create_searcher(*, pipeline, scoring, cv, n_trials, timeout)
¶
Source code in src/sklab/_search/sklearn.py
__init__(param_distributions, n_iter=None, scoring=None, cv=None, refit=True, n_jobs=None, random_state=None, verbose=0, pre_dispatch='2*n_jobs', error_score='raise')
¶
sklab.search.OptunaConfig
dataclass
¶
Configuration for Optuna-based hyperparameter search.
Use this to configure how Experiment.search() explores the hyperparameter
space using Optuna's optimization algorithms.
Parameters¶
search_space
A callable that defines the hyperparameter search space. Receives an
Optuna Trial_ object and returns a mapping of parameter names to
suggested values. Use trial.suggest_* methods to sample values.
Example::
def search_space(trial: Trial) -> dict[str, Any]:
return {
"classifier__C": trial.suggest_float("C", 0.01, 100, log=True),
"classifier__kernel": trial.suggest_categorical("kernel", ["rbf", "linear"]),
}
n_trials Number of trials to run. Each trial evaluates one hyperparameter configuration. Default: 50.
direction
Optimization direction: Direction.MAXIMIZE (default) or
Direction.MINIMIZE. Since Direction is a StrEnum, you can
also pass "maximize" or "minimize" directly. Use maximize for
metrics like accuracy; minimize for metrics like log_loss.
callbacks
Optional sequence of callbacks invoked after each trial completes.
Each callback receives the Study_ and FrozenTrial_ objects.
Useful for early stopping, logging, or custom pruning logic.
See Optuna callbacks tutorial_.
study_factory
Optional factory function to create a custom Study_. Receives
direction as a keyword argument and returns a Study. Use this
when you need:
- A custom `sampler`_ (e.g., ``RandomSampler``, ``CmaEsSampler``)
- A custom `pruner`_ (e.g., ``HyperbandPruner``)
- Persistent storage (database URL for resumable studies)
- A named study for tracking across runs
Example::
def my_study_factory(direction: str) -> Study:
return optuna.create_study(
direction=direction,
sampler=optuna.samplers.TPESampler(seed=42),
pruner=optuna.pruners.HyperbandPruner(),
storage="sqlite:///optuna.db",
study_name="my-experiment",
load_if_exists=True,
)
If None, uses ``optuna.create_study(direction=direction)`` with
defaults (TPE sampler, median pruner, in-memory storage).
scoring
Scorer to use for evaluating trials. If None, uses the first scorer
from the Experiment's scoring. Can be a string (e.g., "accuracy"),
a ScorerName enum, or a callable.
References¶
.. _Trial: https://optuna.readthedocs.io/en/stable/reference/generated/optuna.trial.Trial.html .. _Study: https://optuna.readthedocs.io/en/stable/reference/generated/optuna.study.Study.html .. _FrozenTrial: https://optuna.readthedocs.io/en/stable/reference/generated/optuna.trial.FrozenTrial.html .. _sampler: https://optuna.readthedocs.io/en/stable/reference/samplers/index.html .. _pruner: https://optuna.readthedocs.io/en/stable/reference/pruners.html .. _Optuna callbacks tutorial: https://optuna.readthedocs.io/en/stable/tutorial/20_recipes/007_optuna_callback.html
Source code in src/sklab/_search/optuna.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
create_searcher(*, pipeline, scoring, cv, n_trials, timeout)
¶
Source code in src/sklab/_search/optuna.py
__init__(search_space, n_trials=50, direction=Direction.MAXIMIZE, callbacks=None, study_factory=None, scoring=None)
¶
sklab.search.SearcherProtocol
¶
Bases:
Minimal interface required by Experiment.search.
Source code in src/sklab/adapters/search.py
sklab.search.SearchConfigProtocol
¶
Bases:
Config that can build a searcher for Experiment.search.