From ab51ce6f75930bd5430a866ba33ecca546f9adfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Kir=C3=A1ly?= Date: Sat, 23 Aug 2025 16:07:34 +0200 Subject: [PATCH 1/5] docs --- .../integrations/sklearn/opt_cv.py | 24 +++++++++++++++++-- .../integrations/sktime/_forecasting.py | 22 ++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/hyperactive/integrations/sklearn/opt_cv.py b/src/hyperactive/integrations/sklearn/opt_cv.py index 47bc2f5b..14c8cefb 100644 --- a/src/hyperactive/integrations/sklearn/opt_cv.py +++ b/src/hyperactive/integrations/sklearn/opt_cv.py @@ -15,7 +15,21 @@ class OptCV(BaseEstimator, _BestEstimator_, Checks): - """Tuning via any optimizer in the hyperactive API. + """Tuning an sklearn estimator via any optimizer in the hyperactive API. + + ``OptCV`` uses any available tuning engine from ``hyperactive`` + to tune an sklearn estimator via cross-validation. + + It passes cross-validation results as scores to the tuning engine, + which identifies the best hyperparameters. + + Any available tuning engine from hyperactive can be used, for example: + + * grid search - ``from hyperactive.opt import GridSearchSk as GridSearch`` + * hill climbing - ``from hyperactive.opt import HillClimbing`` + * optuna search - ``from hyperactive.opt.optuna import TPEOptimizer`` + + Configuration of the tuning engine is as per the respective documentation. Parameters ---------- @@ -40,7 +54,13 @@ class OptCV(BaseEstimator, _BestEstimator_, Checks): Example ------- - Tuning sklearn SVC via grid search + Any available tuning engine from hyperactive can be used, for example: + + * grid search - ``from hyperactive.opt import GridSearchSk as GridSearch`` + * hill climbing - ``from hyperactive.opt import HillClimbing`` + * optuna search - ``from hyperactive.opt.optuna import TPEOptimizer`` + + For illustration, we use grid search, this can be replaced by any other optimizer. 1. defining the tuned estimator: >>> from sklearn.svm import SVC diff --git a/src/hyperactive/integrations/sktime/_forecasting.py b/src/hyperactive/integrations/sktime/_forecasting.py index 8fe6856e..9a4a0fb6 100644 --- a/src/hyperactive/integrations/sktime/_forecasting.py +++ b/src/hyperactive/integrations/sktime/_forecasting.py @@ -16,6 +16,20 @@ class ForecastingOptCV(_DelegatedForecaster): """Tune an sktime forecaster via any optimizer in the hyperactive API. + ``ForecastingOptCV`` uses any available tuning engine from ``hyperactive`` + to tune a forecaster by backtesting. + + It passes backtesting results as scores to the tuning engine, + which identifies the best hyperparameters. + + Any available tuning engine from hyperactive can be used, for example: + + * grid search - ``from hyperactive.opt import GridSearchSk as GridSearch`` + * hill climbing - ``from hyperactive.opt import HillClimbing`` + * optuna search - ``from hyperactive.opt.optuna import TPEOptimizer`` + + Configuration of the tuning engine is as per the respective documentation. + Parameters ---------- forecaster : sktime forecaster, BaseForecaster instance or interface compatible @@ -124,7 +138,13 @@ class ForecastingOptCV(_DelegatedForecaster): Example ------- - Tuning an sktime forecaster via grid search + Any available tuning engine from hyperactive can be used, for example: + + * grid search - ``from hyperactive.opt import GridSearchSk as GridSearch`` + * hill climbing - ``from hyperactive.opt import HillClimbing`` + * optuna search - ``from hyperactive.opt.optuna import TPEOptimizer`` + + For illustration, we use grid search, this can be replaced by any other optimizer. 1. defining the tuned estimator: >>> from sktime.forecasting.naive import NaiveForecaster From b0d7cbbbcf27569405256cce44cf92a6c07f6a4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Kir=C3=A1ly?= Date: Sat, 23 Aug 2025 16:13:58 +0200 Subject: [PATCH 2/5] improvements --- .../integrations/sklearn/opt_cv.py | 20 +++++++++++++---- .../integrations/sktime/_forecasting.py | 22 ++++++++++++++----- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/hyperactive/integrations/sklearn/opt_cv.py b/src/hyperactive/integrations/sklearn/opt_cv.py index 14c8cefb..43029099 100644 --- a/src/hyperactive/integrations/sklearn/opt_cv.py +++ b/src/hyperactive/integrations/sklearn/opt_cv.py @@ -25,15 +25,27 @@ class OptCV(BaseEstimator, _BestEstimator_, Checks): Any available tuning engine from hyperactive can be used, for example: - * grid search - ``from hyperactive.opt import GridSearchSk as GridSearch`` + * grid search - ``from hyperactive.opt import GridSearchSk as GridSearch``, + this results in the same algorithm as ``GridSearchCV`` * hill climbing - ``from hyperactive.opt import HillClimbing`` - * optuna search - ``from hyperactive.opt.optuna import TPEOptimizer`` + * optuna parzen-tree search - ``from hyperactive.opt.optuna import TPEOptimizer`` Configuration of the tuning engine is as per the respective documentation. + Formally, ``OptCV`` does the following: + + In ``fit``, wraps the ``estimator``, ``scoring``, and other parameters + into a ``SklearnCvExperiment`` instance, which is passed to the optimizer + ``optimizer`` as the ``experiment`` argument. + Optimal parameters are then obtained from ``optimizer.solve``, and set + as ``best_params_`` and ``best_estimator_`` attributes. + + In ``predict`` and ``predict``-like methods, calls the respective method + of the ``best_estimator_`` if ``refit=True``. + Parameters ---------- - estimator : SklearnBaseEstimator + estimator : sklearn BaseEstimator The estimator to be tuned. optimizer : hyperactive BaseOptimizer The optimizer to be used for hyperparameter search. @@ -58,7 +70,7 @@ class OptCV(BaseEstimator, _BestEstimator_, Checks): * grid search - ``from hyperactive.opt import GridSearchSk as GridSearch`` * hill climbing - ``from hyperactive.opt import HillClimbing`` - * optuna search - ``from hyperactive.opt.optuna import TPEOptimizer`` + * optuna parzen-tree search - ``from hyperactive.opt.optuna import TPEOptimizer`` For illustration, we use grid search, this can be replaced by any other optimizer. diff --git a/src/hyperactive/integrations/sktime/_forecasting.py b/src/hyperactive/integrations/sktime/_forecasting.py index 9a4a0fb6..ca8ed58f 100644 --- a/src/hyperactive/integrations/sktime/_forecasting.py +++ b/src/hyperactive/integrations/sktime/_forecasting.py @@ -24,12 +24,24 @@ class ForecastingOptCV(_DelegatedForecaster): Any available tuning engine from hyperactive can be used, for example: - * grid search - ``from hyperactive.opt import GridSearchSk as GridSearch`` + * grid search - ``from hyperactive.opt import GridSearchSk as GridSearch``, + this results in the same algorithm as ``ForecastingGridSearchCV`` * hill climbing - ``from hyperactive.opt import HillClimbing`` - * optuna search - ``from hyperactive.opt.optuna import TPEOptimizer`` + * optuna parzen-tree search - ``from hyperactive.opt.optuna import TPEOptimizer`` Configuration of the tuning engine is as per the respective documentation. + Formally, ``ForecastingOptCV`` does the following: + + In ``fit``, wraps the ``forecaster``, ``scoring``, and other parameters + into a ``SktimeForecastingExperiment`` instance, which is passed to the optimizer + ``optimizer`` as the ``experiment`` argument. + Optimal parameters are then obtained from ``optimizer.solve``, and set + as ``best_params_`` and ``best_forecaster_`` attributes. + + In ``predict`` and ``predict``-like methods, calls the respective method + of the ``best_forecaster_`` if ``refit=True``. + Parameters ---------- forecaster : sktime forecaster, BaseForecaster instance or interface compatible @@ -142,7 +154,7 @@ class ForecastingOptCV(_DelegatedForecaster): * grid search - ``from hyperactive.opt import GridSearchSk as GridSearch`` * hill climbing - ``from hyperactive.opt import HillClimbing`` - * optuna search - ``from hyperactive.opt.optuna import TPEOptimizer`` + * optuna parzen-tree search - ``from hyperactive.opt.optuna import TPEOptimizer`` For illustration, we use grid search, this can be replaced by any other optimizer. @@ -171,9 +183,9 @@ class ForecastingOptCV(_DelegatedForecaster): ForecastingOptCV(...) >>> y_pred = tuned_naive.predict() - 3. obtaining best parameters and best estimator + 3. obtaining best parameters and best forecaster >>> best_params = tuned_naive.best_params_ - >>> best_estimator = tuned_naive.best_forecaster_ + >>> best_forecaster = tuned_naive.best_forecaster_ """ _tags = { From 334c1e7e2e37022b967599428810e3b21401700d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Kir=C3=A1ly?= Date: Sat, 23 Aug 2025 16:15:59 +0200 Subject: [PATCH 3/5] api --- src/hyperactive/integrations/sklearn/opt_cv.py | 2 +- src/hyperactive/integrations/sktime/_forecasting.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hyperactive/integrations/sklearn/opt_cv.py b/src/hyperactive/integrations/sklearn/opt_cv.py index 43029099..0ef68eb0 100644 --- a/src/hyperactive/integrations/sklearn/opt_cv.py +++ b/src/hyperactive/integrations/sklearn/opt_cv.py @@ -15,7 +15,7 @@ class OptCV(BaseEstimator, _BestEstimator_, Checks): - """Tuning an sklearn estimator via any optimizer in the hyperactive API. + """Tuning an sklearn estimator via any optimizer in the hyperactive toolbox. ``OptCV`` uses any available tuning engine from ``hyperactive`` to tune an sklearn estimator via cross-validation. diff --git a/src/hyperactive/integrations/sktime/_forecasting.py b/src/hyperactive/integrations/sktime/_forecasting.py index ca8ed58f..8910e4fb 100644 --- a/src/hyperactive/integrations/sktime/_forecasting.py +++ b/src/hyperactive/integrations/sktime/_forecasting.py @@ -14,7 +14,7 @@ class ForecastingOptCV(_DelegatedForecaster): - """Tune an sktime forecaster via any optimizer in the hyperactive API. + """Tune an sktime forecaster via any optimizer in the hyperactive toolbox. ``ForecastingOptCV`` uses any available tuning engine from ``hyperactive`` to tune a forecaster by backtesting. From 546d9d7b81bd74c2527d691b16572223ec9de723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Kir=C3=A1ly?= Date: Sat, 23 Aug 2025 16:17:50 +0200 Subject: [PATCH 4/5] lint --- src/hyperactive/integrations/sklearn/opt_cv.py | 2 +- src/hyperactive/integrations/sktime/_forecasting.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hyperactive/integrations/sklearn/opt_cv.py b/src/hyperactive/integrations/sklearn/opt_cv.py index 0ef68eb0..92a09cad 100644 --- a/src/hyperactive/integrations/sklearn/opt_cv.py +++ b/src/hyperactive/integrations/sklearn/opt_cv.py @@ -24,7 +24,7 @@ class OptCV(BaseEstimator, _BestEstimator_, Checks): which identifies the best hyperparameters. Any available tuning engine from hyperactive can be used, for example: - + * grid search - ``from hyperactive.opt import GridSearchSk as GridSearch``, this results in the same algorithm as ``GridSearchCV`` * hill climbing - ``from hyperactive.opt import HillClimbing`` diff --git a/src/hyperactive/integrations/sktime/_forecasting.py b/src/hyperactive/integrations/sktime/_forecasting.py index 8910e4fb..9b450cc9 100644 --- a/src/hyperactive/integrations/sktime/_forecasting.py +++ b/src/hyperactive/integrations/sktime/_forecasting.py @@ -23,7 +23,7 @@ class ForecastingOptCV(_DelegatedForecaster): which identifies the best hyperparameters. Any available tuning engine from hyperactive can be used, for example: - + * grid search - ``from hyperactive.opt import GridSearchSk as GridSearch``, this results in the same algorithm as ``ForecastingGridSearchCV`` * hill climbing - ``from hyperactive.opt import HillClimbing`` From 05528defeb357cb6800bb762c49ac88f2de2ded2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Kir=C3=A1ly?= Date: Sat, 23 Aug 2025 16:22:03 +0200 Subject: [PATCH 5/5] formatting --- src/hyperactive/integrations/sklearn/opt_cv.py | 13 ++++++++----- src/hyperactive/integrations/sktime/_forecasting.py | 13 ++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/hyperactive/integrations/sklearn/opt_cv.py b/src/hyperactive/integrations/sklearn/opt_cv.py index 92a09cad..2f0366fe 100644 --- a/src/hyperactive/integrations/sklearn/opt_cv.py +++ b/src/hyperactive/integrations/sklearn/opt_cv.py @@ -34,11 +34,14 @@ class OptCV(BaseEstimator, _BestEstimator_, Checks): Formally, ``OptCV`` does the following: - In ``fit``, wraps the ``estimator``, ``scoring``, and other parameters - into a ``SklearnCvExperiment`` instance, which is passed to the optimizer - ``optimizer`` as the ``experiment`` argument. - Optimal parameters are then obtained from ``optimizer.solve``, and set - as ``best_params_`` and ``best_estimator_`` attributes. + In ``fit``: + + * wraps the ``estimator``, ``scoring``, and other parameters + into a ``SklearnCvExperiment`` instance, which is passed to the optimizer + ``optimizer`` as the ``experiment`` argument. + * Optimal parameters are then obtained from ``optimizer.solve``, and set + as ``best_params_`` and ``best_estimator_`` attributes. + * If ``refit=True``, ``best_estimator_`` is fitted to the entire ``X`` and ``y``. In ``predict`` and ``predict``-like methods, calls the respective method of the ``best_estimator_`` if ``refit=True``. diff --git a/src/hyperactive/integrations/sktime/_forecasting.py b/src/hyperactive/integrations/sktime/_forecasting.py index 9b450cc9..82f56b74 100644 --- a/src/hyperactive/integrations/sktime/_forecasting.py +++ b/src/hyperactive/integrations/sktime/_forecasting.py @@ -33,11 +33,14 @@ class ForecastingOptCV(_DelegatedForecaster): Formally, ``ForecastingOptCV`` does the following: - In ``fit``, wraps the ``forecaster``, ``scoring``, and other parameters - into a ``SktimeForecastingExperiment`` instance, which is passed to the optimizer - ``optimizer`` as the ``experiment`` argument. - Optimal parameters are then obtained from ``optimizer.solve``, and set - as ``best_params_`` and ``best_forecaster_`` attributes. + In ``fit``: + + * wraps the ``forecaster``, ``scoring``, and other parameters + into a ``SktimeForecastingExperiment`` instance, which is passed to the optimizer + ``optimizer`` as the ``experiment`` argument. + * Optimal parameters are then obtained from ``optimizer.solve``, and set + as ``best_params_`` and ``best_forecaster_`` attributes. + * If ``refit=True``, ``best_forecaster_`` is fitted to the entire ``y`` and ``X``. In ``predict`` and ``predict``-like methods, calls the respective method of the ``best_forecaster_`` if ``refit=True``.