|
| 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 | +``` |
0 commit comments