From 3abc5ce4cebfcdeadc2757a3ce5c7736141e42a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Kir=C3=A1ly?= Date: Sun, 21 Sep 2025 13:19:16 +0200 Subject: [PATCH 1/4] examples sktime sklearn --- examples/integrations/README.md | 100 ++++++++++++++++++ .../integrations/sklearn_classif_example.py | 21 ++++ .../sktime_forecasting_example.py | 29 +++++ 3 files changed, 150 insertions(+) create mode 100644 examples/integrations/README.md create mode 100644 examples/integrations/sklearn_classif_example.py create mode 100644 examples/integrations/sktime_forecasting_example.py diff --git a/examples/integrations/README.md b/examples/integrations/README.md new file mode 100644 index 00000000..507ca211 --- /dev/null +++ b/examples/integrations/README.md @@ -0,0 +1,100 @@ +# Integrations to AI toolboxes + +This directory contains examples for estimator level integration with +common AI toolbox libraries such as `scikit-learn` or `sktime`. + +## Quick Start + +Run any example directly: +```bash +python sklearn_classif_example.py +python sktime_forecasting_example.py +python sktime_tsc_example.py +``` + +Requires `scikit-learn` resp `sktime` installed. + +## Available Integrations + +| Integration | Class name | +|-----------|-------------------| +| `sklearn` classifier or regressor tuner | [OptCV](sklearn_classif_example.py) | +| `sktime` forecasting tuner | [ForecastingOptCV](sktime_forecasting_example.py) | +| `sktime` time series classifier tuner | [TSCOptCV](sktime_tsc_example.py) | + +## Integration with sklearn + +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 parzen-tree search - ``from hyperactive.opt.optuna import TPEOptimizer`` + +```python +# For illustration, we use grid search, this can be replaced by any other optimizer. + +# 1. defining the tuned estimator: +from sklearn.svm import SVC +from hyperactive.integrations.sklearn import OptCV +from hyperactive.opt import GridSearchSk as GridSearch + +param_grid = {"kernel": ["linear", "rbf"], "C": [1, 10]} +tuned_svc = OptCV(SVC(), GridSearch(param_grid)) + +# 2. fitting the tuned estimator: +from sklearn.datasets import load_iris +from sklearn.model_selection import train_test_split +X, y = load_iris(return_X_y=True) +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) + +tuned_svc.fit(X_train, y_train) + +# 3. obtaining predictions +y_pred = tuned_svc.predict(X_test) + +# 4. obtaining best parameters and best estimator +best_params = tuned_svc.best_params_ +best_estimator = tuned_svc.best_estimator_ +``` + +## Integration with sktime - forecasting + +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 parzen-tree search - ``from hyperactive.opt.optuna import TPEOptimizer`` + +For illustration, we use grid search, this can be replaced by any other optimizer. + +```python +# 1. defining the tuned estimator: +from sktime.forecasting.naive import NaiveForecaster +from sktime.split import ExpandingWindowSplitter +from hyperactive.integrations.sktime import ForecastingOptCV +from hyperactive.opt import GridSearchSk as GridSearch + +param_grid = {"strategy": ["mean", "last", "drift"]} +tuned_naive = ForecastingOptCV( + NaiveForecaster(), + GridSearch(param_grid), + cv=ExpandingWindowSplitter( + initial_window=12, step_length=3, fh=range(1, 13) + ), +) + +# 2. fitting the tuned estimator: +from sktime.datasets import load_airline +from sktime.split import temporal_train_test_split +y = load_airline() +y_train, y_test = temporal_train_test_split(y, test_size=12) + +tuned_naive.fit(y_train, fh=range(1, 13)) + +# 3. obtaining predictions +y_pred = tuned_naive.predict() + +# 4. obtaining best parameters and best forecaster +best_params = tuned_naive.best_params_ +best_forecaster = tuned_naive.best_forecaster_ +``` diff --git a/examples/integrations/sklearn_classif_example.py b/examples/integrations/sklearn_classif_example.py new file mode 100644 index 00000000..bfab28bb --- /dev/null +++ b/examples/integrations/sklearn_classif_example.py @@ -0,0 +1,21 @@ +from sklearn.svm import SVC +from hyperactive.integrations.sklearn import OptCV +from hyperactive.opt import GridSearchSk as GridSearch + +param_grid = {"kernel": ["linear", "rbf"], "C": [1, 10]} +tuned_svc = OptCV(SVC(), GridSearch(param_grid)) + +# 2. fitting the tuned estimator: +from sklearn.datasets import load_iris +from sklearn.model_selection import train_test_split +X, y = load_iris(return_X_y=True) +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) + +tuned_svc.fit(X_train, y_train) + +# 3. obtaining predictions +y_pred = tuned_svc.predict(X_test) + +# 4. obtaining best parameters and best estimator +best_params = tuned_svc.best_params_ +best_estimator = tuned_svc.best_estimator_ diff --git a/examples/integrations/sktime_forecasting_example.py b/examples/integrations/sktime_forecasting_example.py new file mode 100644 index 00000000..17800566 --- /dev/null +++ b/examples/integrations/sktime_forecasting_example.py @@ -0,0 +1,29 @@ +# 1. defining the tuned estimator: +from sktime.forecasting.naive import NaiveForecaster +from sktime.split import ExpandingWindowSplitter +from hyperactive.integrations.sktime import ForecastingOptCV +from hyperactive.opt import GridSearchSk as GridSearch + +param_grid = {"strategy": ["mean", "last", "drift"]} +tuned_naive = ForecastingOptCV( + NaiveForecaster(), + GridSearch(param_grid), + cv=ExpandingWindowSplitter( + initial_window=12, step_length=3, fh=range(1, 13) + ), +) + +# 2. fitting the tuned estimator: +from sktime.datasets import load_airline +from sktime.split import temporal_train_test_split +y = load_airline() +y_train, y_test = temporal_train_test_split(y, test_size=12) + +tuned_naive.fit(y_train, fh=range(1, 13)) + +# 3. obtaining predictions +y_pred = tuned_naive.predict() + +# 4. obtaining best parameters and best forecaster +best_params = tuned_naive.best_params_ +best_forecaster = tuned_naive.best_forecaster_ From 8acfb7f9c50236de8704e4562cddf506faf5ebdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Kir=C3=A1ly?= Date: Sun, 21 Sep 2025 13:20:11 +0200 Subject: [PATCH 2/4] Update README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5aebf96c..685bd7c5 100644 --- a/README.md +++ b/README.md @@ -237,9 +237,11 @@ Hyperactive features a collection of optimization algorithms that can be used fo - Machine Learning: + AI and Machine Learning: From 88808e3857648237fa40e776af5b7772e3200131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Kir=C3=A1ly?= Date: Sun, 21 Sep 2025 13:22:30 +0200 Subject: [PATCH 3/4] tsc --- examples/integrations/README.md | 47 ++++++++++++++++++++- examples/integrations/sktime_tsc_example.py | 30 +++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 examples/integrations/sktime_tsc_example.py diff --git a/examples/integrations/README.md b/examples/integrations/README.md index 507ca211..ef674be1 100644 --- a/examples/integrations/README.md +++ b/examples/integrations/README.md @@ -5,7 +5,9 @@ common AI toolbox libraries such as `scikit-learn` or `sktime`. ## Quick Start -Run any example directly: +For full code, see below. + +You can also run any example directly: ```bash python sklearn_classif_example.py python sktime_forecasting_example.py @@ -98,3 +100,46 @@ y_pred = tuned_naive.predict() best_params = tuned_naive.best_params_ best_forecaster = tuned_naive.best_forecaster_ ``` + +## Integration with sktime - time series classification + +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 parzen-tree search - ``from hyperactive.opt.optuna import TPEOptimizer`` + +For illustration, we use grid search, this can be replaced by any other optimizer. + +```python +# 1. defining the tuned estimator: +from sktime.classification.dummy import DummyClassifier +from sklearn.model_selection import KFold +from hyperactive.integrations.sktime import TSCOptCV +from hyperactive.opt import GridSearchSk as GridSearch + +param_grid = {"strategy": ["most_frequent", "stratified"]} +tuned_naive = TSCOptCV( + DummyClassifier(), + GridSearch(param_grid), + cv=KFold(n_splits=2, shuffle=False), +) + +# 2. fitting the tuned estimator: +from sktime.datasets import load_unit_test +X_train, y_train = load_unit_test( + return_X_y=True, split="TRAIN", return_type="pd-multiindex" +) +X_test, _ = load_unit_test( + return_X_y=True, split="TEST", return_type="pd-multiindex" +) + +tuned_naive.fit(X_train, y_train) + +# 3. obtaining predictions +y_pred = tuned_naive.predict(X_test) + +# 4. obtaining best parameters and best estimator +best_params = tuned_naive.best_params_ +best_classifier = tuned_naive.best_estimator_ +``` diff --git a/examples/integrations/sktime_tsc_example.py b/examples/integrations/sktime_tsc_example.py new file mode 100644 index 00000000..ffbdf856 --- /dev/null +++ b/examples/integrations/sktime_tsc_example.py @@ -0,0 +1,30 @@ +# 1. defining the tuned estimator: +from sktime.classification.dummy import DummyClassifier +from sklearn.model_selection import KFold +from hyperactive.integrations.sktime import TSCOptCV +from hyperactive.opt import GridSearchSk as GridSearch + +param_grid = {"strategy": ["most_frequent", "stratified"]} +tuned_naive = TSCOptCV( + DummyClassifier(), + GridSearch(param_grid), + cv=KFold(n_splits=2, shuffle=False), +) + +# 2. fitting the tuned estimator: +from sktime.datasets import load_unit_test +X_train, y_train = load_unit_test( + return_X_y=True, split="TRAIN", return_type="pd-multiindex" +) +X_test, _ = load_unit_test( + return_X_y=True, split="TEST", return_type="pd-multiindex" +) + +tuned_naive.fit(X_train, y_train) + +# 3. obtaining predictions +y_pred = tuned_naive.predict(X_test) + +# 4. obtaining best parameters and best estimator +best_params = tuned_naive.best_params_ +best_classifier = tuned_naive.best_estimator_ From b6e9f1f01a4c90adca889834c54f80b1a41aed7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Kir=C3=A1ly?= Date: Sun, 21 Sep 2025 13:25:30 +0200 Subject: [PATCH 4/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 685bd7c5..13dcf1e6 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,7 @@ Hyperactive features a collection of optimization algorithms that can be used fo - Tested and Supported Packages + Framework Integrations