Optimizer API¶
run_study¶
h2ml.optimization.optimizer.run_study(name, X, y, task='classification', metric=None, n_trials=50, n_splits=5, random_state=42, study_name=None, verbose=True, coords=None, spatial=None, n_hpo_repeats=1, fixed_params=None, y_true=None, inverse_fn=None, train_target_fn=None, _splitter=None, estimator=None, requires_scaling=None, selection_scoring='pooled')
¶
Run an Optuna hyperparameter optimization study for a registered model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Model class name (e.g. 'RandomForestClassifier'). Must be registered in CLASSIFIER_OPT_PARAMS or REGRESSOR_OPT_PARAMS. |
required |
X
|
ndarray
|
Feature matrix as numpy array (n_samples, n_features). |
required |
y
|
ndarray
|
Target array (n_samples,). |
required |
task
|
str
|
'classification' or 'regression'. |
'classification'
|
metric
|
Optional[str]
|
Scoring metric for the objective. Must be a key in CLF_METRICS (classification) or REG_METRICS (regression). Defaults to 'AUC' for classification, 'R2' for regression. All metrics are maximised internally (error metrics are negated). |
None
|
n_trials
|
int
|
Total number of Optuna trials across all repeats (default 50). Divided evenly: trials_per_repeat = max(1, n_trials // n_hpo_repeats). |
50
|
n_splits
|
int
|
CV folds inside each trial objective (default 5). |
5
|
random_state
|
int
|
Base seed for reproducibility. Each repeat uses random_state + repeat_idx for both the TPE sampler and fold splitter (default 42). |
42
|
study_name
|
Optional[str]
|
Optional Optuna study name for logging/storage. |
None
|
verbose
|
bool
|
If True, log trial progress. |
True
|
n_hpo_repeats
|
int
|
Number of independent HPO runs with different fold seeds (default 1). Each repeat draws fold splits from a different random seed, amortising the risk of an unlucky single split. The study with the highest best_value is returned. |
1
|
estimator
|
Any
|
Optional unfitted estimator to tune. Each trial clones it and sets the sampled params on top, keeping its other constructor arguments. Its class must match name's registry entry, which still supplies the search space. When None, trials are built from the registry class with only the sampled params. |
None
|
requires_scaling
|
Optional[bool]
|
Scale features inside folds; defaults to the registry entry's requires_scaling when None. |
None
|
selection_scoring
|
str
|
"pooled" scores regression trials once on all out-of-fold rows; "fold_mean" averages per-fold scores. Classification always uses the fold mean. |
'pooled'
|
Returns:
| Type | Description |
|---|---|
Study
|
optuna.Study with best_params, best_value, and full trial history. |
Study
|
When n_hpo_repeats > 1, this is the repeat whose best_value was highest. |
Study
|
If every trial was pruned or failed, a warning is logged and the returned |
Study
|
study's best_params and best_value raise ValueError. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the model is not registered. |
ValueError
|
If the model is registered but disabled, or metric is invalid. |
Examples:
optimize_all¶
h2ml.optimization.optimizer.optimize_all(names, X, y, task='classification', metric=None, n_trials=50, n_splits=5, random_state=42, verbose=False)
¶
Run optimization studies for multiple models.
Returns a dict keyed by model name. Models not found in the registry are skipped with a warning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
list[str]
|
List of model class names to optimize. |
required |
X
|
ndarray
|
Feature matrix. |
required |
y
|
ndarray
|
Target vector. |
required |
task
|
str
|
"classification" or "regression". |
'classification'
|
metric
|
Optional[str]
|
Metric to optimise; defaults to the task's default, as in run_study(). |
None
|
n_trials
|
int
|
Optuna trials per model. |
50
|
n_splits
|
int
|
CV folds per trial (k-fold, stratified for classification; no spatial splitting). |
5
|
random_state
|
int
|
Seed for folds and the sampler. |
42
|
verbose
|
bool
|
Log study progress. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Study]
|
Dict[model_name, optuna.Study] |
Examples:
Available metrics¶
| Dict | Keys |
|---|---|
CLF_METRICS |
"AUC", "AUCPR", "LogLoss", "F1", "Brier" |
REG_METRICS |
"R2", "MAE", "RMSE", "PoissonDev" |
POOLED_REG_METRICS |
the same regression keys, scored once over all out-of-fold rows |
All metrics are maximised internally — error metrics (LogLoss, Brier, MAE, RMSE, PoissonDev) are negated before optimisation and displayed with their natural (positive) values. Regression trials use the pooled scorers by default (selection_scoring="pooled") and the fold mean with "fold_mean".