Autoregressive Modelling in Sklearn

In a lot of cases, traditional time series models work well, but there are many traditional machine learning algorithms that work very well on tabular datasets and it would be waste not to leverage their power for time-series forecast. To enable its use we developed SklearnWrapper

SklearnWrapper

Allows you use Sklearn-API regressors as autoregressive models for time-series predictions. In terms of usage, there is one difference between the rest of the wrappers and SklearnWrapper. Since the model is provided by package user and we don’t know parameters of the model ahead - usage of factory function get_sklearn_wrapper is needed. You can put any sklearn-compatible regressor to the function and it will return SklearnWrapper class

f2101e88b4dc46cc9f65075659276671

[1]:
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('seaborn')
plt.rcParams['figure.figsize'] = [12, 6]
[2]:
from hcrystalball.utils import get_sales_data

df = get_sales_data(n_dates=365*2,
                    n_assortments=1,
                    n_states=1,
                    n_stores=1)
X, y = pd.DataFrame(index=df.index), df['Sales']
[3]:
from hcrystalball.wrappers import get_sklearn_wrapper

Sklearn’s or sklearn’s compatible regressor

You can provide any parameter for original regressor model. It doesn’t need to be just Sklearn model, only Sklearn API is required. You define as first positional argument the wrapping class and than you can mix wrappers parameters (clip_predictions_lower) with wrapped class parameters(n_estimators)

[4]:
from sklearn.ensemble import RandomForestRegressor
[5]:
model = get_sklearn_wrapper(RandomForestRegressor, n_estimators=100, clip_predictions_lower=0., random_state=42)
preds = (model.fit(X[:-10], y[:-10])
         .predict(X[-10:])
         .merge(y, left_index=True, right_index=True, how='outer')
         .tail(50)
)
preds.plot(title=f"MAE:{(preds['Sales']-preds['sklearn']).abs().mean().round(3)}");
[5]:
<AxesSubplot:title={'center':'MAE:3744.396'}, xlabel='Date'>
../../../_images/examples_tutorial_wrappers_02_ar_modelling_in_sklearn_9_1.png