[3]:
# Import to be able to import python package from src
import sys
sys.path.insert(0, '../src')
[4]:
import pandas as pd
import numpy as np
import ontime as on

Models#

This class implement a generic way to load models in onTime. It is compatible with Darts and Scikit-learn with an aime to add other librairies and models soon. Let’s see how to use it.

Let’s generate of random TimeSeries#

[7]:
# Start and end dates
start_date = pd.Timestamp('2022-01-01')
end_date = pd.Timestamp('2022-12-31')

# Make a random walk
ts = on.generators.random_walk().generate(start=start_date, end=end_date)
ts = ts.astype(np.float32)

Using a Darts models#

The model can be loaded from the desired library

[2]:
from darts.models import BlockRNNModel
The `LightGBM` module could not be imported. To enable LightGBM support in Darts, follow the detailed instructions in the installation guide: https://github.com/unit8co/darts/blob/master/INSTALL.md
The `Prophet` module could not be imported. To enable Prophet support in Darts, follow the detailed instructions in the installation guide: https://github.com/unit8co/darts/blob/master/INSTALL.md

Then, it can be used with this generic interface.

[11]:
model = on.Model(BlockRNNModel,
                 input_chunk_length=12,
                 output_chunk_length=6,
                 n_rnn_layers=2,
                 n_epochs=50
                 )

Finally, the training and inference functions are the same than in Scikit Learn for instance :

[13]:
# To train the model
model.fit(ts)
darts.models.forecasting.torch_forecasting_model INFO  Train dataset contains 348 samples.
GPU available: True (mps), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs

  | Name          | Type             | Params
---------------------------------------------------
0 | criterion     | MSELoss          | 0
1 | train_metrics | MetricCollection | 0
2 | val_metrics   | MetricCollection | 0
3 | rnn           | RNN              | 2.0 K
4 | fc            | Sequential       | 156
---------------------------------------------------
2.2 K     Trainable params
0         Non-trainable params
2.2 K     Total params
0.009     Total estimated model params size (MB)
`Trainer.fit` stopped: `max_epochs=50` reached.
[13]:
<ontime.core.modelling.model.Model at 0x2bd0b7a30>
[14]:
# To make a prediction
model.predict(5)
GPU available: True (mps), used: True
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
HPU available: False, using: 0 HPUs
[14]:
<TimeSeries (DataArray) (time: 5, component: 1, sample: 1)>
array([[[-19.62056 ]],

       [[-19.278965]],

       [[-19.425938]],

       [[-19.654451]],

       [[-19.767109]]], dtype=float32)
Coordinates:
  * time       (time) datetime64[ns] 2023-01-01 2023-01-02 ... 2023-01-05
  * component  (component) object 'random_walk'
Dimensions without coordinates: sample
Attributes:
    static_covariates:  None
    hierarchy:          None

Using a Scikit-learn API compatible models#

[6]:
from sklearn.neural_network import MLPRegressor
[7]:
model = on.Model(MLPRegressor,
                 lags=30)
model.fit(ts)
model.predict(5)
/Users/fred.montet/Library/Caches/pypoetry/virtualenvs/ontime-FpQu8-YN-py3.10/lib/python3.10/site-packages/sklearn/neural_network/_multilayer_perceptron.py:691: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (200) reached and the optimization hasn't converged yet.
  warnings.warn(
[7]:
<TimeSeries (DataArray) (time: 5, component: 1, sample: 1)>
array([[[31.41836973]],

       [[30.42709606]],

       [[30.61009553]],

       [[30.309712  ]],

       [[29.84004834]]])
Coordinates:
  * time       (time) datetime64[ns] 2023-01-01 2023-01-02 ... 2023-01-05
  * component  (component) object 'pred'
Dimensions without coordinates: sample
Attributes:
    static_covariates:  None
    hierarchy:          None