Skip to content

Commit 2842478

Browse files
authored
[DOC] Examples for estimator integration with AI toolboxes (#193)
I just checked the links, it seems there are no examples for estimator integration with AI toolboxes linked to from the readme, and the jupyter notebook is not directly displayed. As a quick fix, I have added and linked the integration examples from the readme.
1 parent b68efc5 commit 2842478

5 files changed

Lines changed: 230 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ Hyperactive features a collection of optimization algorithms that can be used fo
179179
<img src="./docs/images/blue.jpg"/>
180180
</td>
181181
<td>
182-
<strong>Tested and Supported Packages</strong>
182+
<strong>Framework Integrations</strong>
183183
<img src="./docs/images/blue.jpg"/>
184184
</td>
185185
<td>
@@ -237,9 +237,11 @@ Hyperactive features a collection of optimization algorithms that can be used fo
237237
</ul>
238238
</td>
239239
<td>
240-
<a><b>Machine Learning:</b></a>
240+
<a><b>AI and Machine Learning:</b></a>
241241
<ul>
242-
<li><a href="./examples/sklearn/README.md">Scikit-learn</a></li>
242+
<li><a href="./examples/integrations/README.md">scikit-learn</a></li>
243+
<li><a href="./examples/integrations/README.md">sktime forecasting</a></li>
244+
<li><a href="./examples/integrations/README.md">sktime time series classification</a></li>
243245
</ul>
244246
</td>
245247
<td>

‎examples/integrations/README.md‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Integrations to AI toolboxes
2+
3+
This directory contains examples for estimator level integration with
4+
common AI toolbox libraries such as `scikit-learn` or `sktime`.
5+
6+
## Quick Start
7+
8+
For full code, see below.
9+
10+
You can also run any example directly:
11+
```bash
12+
python sklearn_classif_example.py
13+
python sktime_forecasting_example.py
14+
python sktime_tsc_example.py
15+
```
16+
17+
Requires `scikit-learn` resp `sktime` installed.
18+
19+
## Available Integrations
20+
21+
| Integration | Class name |
22+
|-----------|-------------------|
23+
| `sklearn` classifier or regressor tuner | [OptCV](sklearn_classif_example.py) |
24+
| `sktime` forecasting tuner | [ForecastingOptCV](sktime_forecasting_example.py) |
25+
| `sktime` time series classifier tuner | [TSCOptCV](sktime_tsc_example.py) |
26+
27+
## Integration with sklearn
28+
29+
Any available tuning engine from hyperactive can be used, for example:
30+
31+
* grid search - ``from hyperactive.opt import GridSearchSk as GridSearch``
32+
* hill climbing - ``from hyperactive.opt import HillClimbing``
33+
* optuna parzen-tree search - ``from hyperactive.opt.optuna import TPEOptimizer``
34+
35+
```python
36+
# For illustration, we use grid search, this can be replaced by any other optimizer.
37+
38+
# 1. defining the tuned estimator:
39+
from sklearn.svm import SVC
40+
from hyperactive.integrations.sklearn import OptCV
41+
from hyperactive.opt import GridSearchSk as GridSearch
42+
43+
param_grid = {"kernel": ["linear", "rbf"], "C": [1, 10]}
44+
tuned_svc = OptCV(SVC(), GridSearch(param_grid))
45+
46+
# 2. fitting the tuned estimator:
47+
from sklearn.datasets import load_iris
48+
from sklearn.model_selection import train_test_split
49+
X, y = load_iris(return_X_y=True)
50+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
51+
52+
tuned_svc.fit(X_train, y_train)
53+
54+
# 3. obtaining predictions
55+
y_pred = tuned_svc.predict(X_test)
56+
57+
# 4. obtaining best parameters and best estimator
58+
best_params = tuned_svc.best_params_
59+
best_estimator = tuned_svc.best_estimator_
60+
```
61+
62+
## Integration with sktime - forecasting
63+
64+
Any available tuning engine from hyperactive can be used, for example:
65+
66+
* grid search - ``from hyperactive.opt import GridSearchSk as GridSearch``
67+
* hill climbing - ``from hyperactive.opt import HillClimbing``
68+
* optuna parzen-tree search - ``from hyperactive.opt.optuna import TPEOptimizer``
69+
70+
For illustration, we use grid search, this can be replaced by any other optimizer.
71+
72+
```python
73+
# 1. defining the tuned estimator:
74+
from sktime.forecasting.naive import NaiveForecaster
75+
from sktime.split import ExpandingWindowSplitter
76+
from hyperactive.integrations.sktime import ForecastingOptCV
77+
from hyperactive.opt import GridSearchSk as GridSearch
78+
79+
param_grid = {"strategy": ["mean", "last", "drift"]}
80+
tuned_naive = ForecastingOptCV(
81+
NaiveForecaster(),
82+
GridSearch(param_grid),
83+
cv=ExpandingWindowSplitter(
84+
initial_window=12, step_length=3, fh=range(1, 13)
85+
),
86+
)
87+
88+
# 2. fitting the tuned estimator:
89+
from sktime.datasets import load_airline
90+
from sktime.split import temporal_train_test_split
91+
y = load_airline()
92+
y_train, y_test = temporal_train_test_split(y, test_size=12)
93+
94+
tuned_naive.fit(y_train, fh=range(1, 13))
95+
96+
# 3. obtaining predictions
97+
y_pred = tuned_naive.predict()
98+
99+
# 4. obtaining best parameters and best forecaster
100+
best_params = tuned_naive.best_params_
101+
best_forecaster = tuned_naive.best_forecaster_
102+
```
103+
104+
## Integration with sktime - time series classification
105+
106+
Any available tuning engine from hyperactive can be used, for example:
107+
108+
* grid search - ``from hyperactive.opt import GridSearchSk as GridSearch``
109+
* hill climbing - ``from hyperactive.opt import HillClimbing``
110+
* optuna parzen-tree search - ``from hyperactive.opt.optuna import TPEOptimizer``
111+
112+
For illustration, we use grid search, this can be replaced by any other optimizer.
113+
114+
```python
115+
# 1. defining the tuned estimator:
116+
from sktime.classification.dummy import DummyClassifier
117+
from sklearn.model_selection import KFold
118+
from hyperactive.integrations.sktime import TSCOptCV
119+
from hyperactive.opt import GridSearchSk as GridSearch
120+
121+
param_grid = {"strategy": ["most_frequent", "stratified"]}
122+
tuned_naive = TSCOptCV(
123+
DummyClassifier(),
124+
GridSearch(param_grid),
125+
cv=KFold(n_splits=2, shuffle=False),
126+
)
127+
128+
# 2. fitting the tuned estimator:
129+
from sktime.datasets import load_unit_test
130+
X_train, y_train = load_unit_test(
131+
return_X_y=True, split="TRAIN", return_type="pd-multiindex"
132+
)
133+
X_test, _ = load_unit_test(
134+
return_X_y=True, split="TEST", return_type="pd-multiindex"
135+
)
136+
137+
tuned_naive.fit(X_train, y_train)
138+
139+
# 3. obtaining predictions
140+
y_pred = tuned_naive.predict(X_test)
141+
142+
# 4. obtaining best parameters and best estimator
143+
best_params = tuned_naive.best_params_
144+
best_classifier = tuned_naive.best_estimator_
145+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from sklearn.svm import SVC
2+
from hyperactive.integrations.sklearn import OptCV
3+
from hyperactive.opt import GridSearchSk as GridSearch
4+
5+
param_grid = {"kernel": ["linear", "rbf"], "C": [1, 10]}
6+
tuned_svc = OptCV(SVC(), GridSearch(param_grid))
7+
8+
# 2. fitting the tuned estimator:
9+
from sklearn.datasets import load_iris
10+
from sklearn.model_selection import train_test_split
11+
X, y = load_iris(return_X_y=True)
12+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
13+
14+
tuned_svc.fit(X_train, y_train)
15+
16+
# 3. obtaining predictions
17+
y_pred = tuned_svc.predict(X_test)
18+
19+
# 4. obtaining best parameters and best estimator
20+
best_params = tuned_svc.best_params_
21+
best_estimator = tuned_svc.best_estimator_
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 1. defining the tuned estimator:
2+
from sktime.forecasting.naive import NaiveForecaster
3+
from sktime.split import ExpandingWindowSplitter
4+
from hyperactive.integrations.sktime import ForecastingOptCV
5+
from hyperactive.opt import GridSearchSk as GridSearch
6+
7+
param_grid = {"strategy": ["mean", "last", "drift"]}
8+
tuned_naive = ForecastingOptCV(
9+
NaiveForecaster(),
10+
GridSearch(param_grid),
11+
cv=ExpandingWindowSplitter(
12+
initial_window=12, step_length=3, fh=range(1, 13)
13+
),
14+
)
15+
16+
# 2. fitting the tuned estimator:
17+
from sktime.datasets import load_airline
18+
from sktime.split import temporal_train_test_split
19+
y = load_airline()
20+
y_train, y_test = temporal_train_test_split(y, test_size=12)
21+
22+
tuned_naive.fit(y_train, fh=range(1, 13))
23+
24+
# 3. obtaining predictions
25+
y_pred = tuned_naive.predict()
26+
27+
# 4. obtaining best parameters and best forecaster
28+
best_params = tuned_naive.best_params_
29+
best_forecaster = tuned_naive.best_forecaster_
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 1. defining the tuned estimator:
2+
from sktime.classification.dummy import DummyClassifier
3+
from sklearn.model_selection import KFold
4+
from hyperactive.integrations.sktime import TSCOptCV
5+
from hyperactive.opt import GridSearchSk as GridSearch
6+
7+
param_grid = {"strategy": ["most_frequent", "stratified"]}
8+
tuned_naive = TSCOptCV(
9+
DummyClassifier(),
10+
GridSearch(param_grid),
11+
cv=KFold(n_splits=2, shuffle=False),
12+
)
13+
14+
# 2. fitting the tuned estimator:
15+
from sktime.datasets import load_unit_test
16+
X_train, y_train = load_unit_test(
17+
return_X_y=True, split="TRAIN", return_type="pd-multiindex"
18+
)
19+
X_test, _ = load_unit_test(
20+
return_X_y=True, split="TEST", return_type="pd-multiindex"
21+
)
22+
23+
tuned_naive.fit(X_train, y_train)
24+
25+
# 3. obtaining predictions
26+
y_pred = tuned_naive.predict(X_test)
27+
28+
# 4. obtaining best parameters and best estimator
29+
best_params = tuned_naive.best_params_
30+
best_classifier = tuned_naive.best_estimator_

0 commit comments

Comments
 (0)