from spotforecast2.multitask import OptunaTask
task = OptunaTask(n_trials_optuna=5, predict_size=24)
print(f"Task: {task.TASK}")
print(f"Optuna trials: {task.config.n_trials_optuna}")Task: optuna
Optuna trials: 5
multitask.OptunaTask(
config=None,
*,
dataframe=None,
data_test=None,
cache_home=None,
log_level=logging.INFO,
**overrides,
)Task 3 — Optuna Bayesian hyperparameter tuning.
Uses Optuna’s TPE sampler to search for optimal LightGBM hyperparameters, then re-fits with the best discovered parameters.
| Name | Description |
|---|---|
| run | Run Optuna Bayesian tuning for all targets. |
Run Optuna Bayesian tuning for all targets.
| Name | Type | Description | Default |
|---|---|---|---|
| show | bool | If True, display prediction figures. |
True |
| search_space | Optional[Callable] | Callable (trial) -> dict. None uses the built-in default. |
None |
| Name | Type | Description |
|---|---|---|
| Dict[str, Any] | Aggregated prediction package. Per-target packages are stored | |
| Dict[str, Any] | on self.results["optuna"]. |
import warnings
from spotforecast2_safe.data.fetch_data import fetch_data, get_package_data_home
from spotforecast2.multitask import OptunaTask
data_home = get_package_data_home()
df = fetch_data(filename=str(data_home / "demo10.csv"))
tiny_df = df.iloc[:500][["A"]]
task = OptunaTask(
n_trials_optuna=2,
predict_size=24,
auto_save_models=False,
lags_consider=[1, 2, 24],
number_folds=2,
verbose=False,
)
task.prepare_data(demo_data=tiny_df)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
result = task.run(show=False)
assert "future_pred" in result
assert result.get("validation_passed") is True
print("OptunaTask.run result keys:", sorted(result.keys()))/tmp/ipykernel_3155/104901005.py:17: DeprecationWarning: Derived pipeline fields (start_download, end_download, data_start, data_end, cov_start, cov_end, end_train_ts, start_train_ts) have moved to task.run_state. Reading them from the config is deprecated and will stop working in the next major release. config.targets continues to hold the user input unchanged; read the resolved list from task.run_state.targets.
task.prepare_data(demo_data=tiny_df)
OptunaTask.run result keys: ['forecaster', 'future_actual', 'future_pred', 'metrics_future', 'metrics_future_one_day', 'metrics_train', 'train_actual', 'train_pred', 'validation_passed']