%%capture
# sktime installation with darts dependencies
! pip install -U sktime
! pip install -U u8darts
Darts Regression models for time series forecasting using sktime
This notebook demonstrates how you can simply use the sktime
to interface the darts
regression models for time series forecasting. We will explore with the current adaptation of the darts
models to the sktime
interface and how to use them for forecasting.
Using sktime gives user the flexibility to use the darts models, providing an easy to extend and use interface for time series forecasting.
Installing the required libraries
# importing needed libraries
import copy
from sklearn.linear_model import LinearRegression
from sktime.datasets import load_longley
from sktime.forecasting.base import ForecastingHorizon
from sktime.forecasting.darts import (
DartsLinearRegressionModel,
DartsRegressionModel,
DartsXGBModel,
)from darts.datasets import WeatherDataset
from sktime.split import temporal_train_test_split
Loading the data
= load_longley()
y, X = temporal_train_test_split(y, X, test_size=4) y_train, y_test, X_train, X_test
Fitting the model
Defining the models
# DartsLinearRegressionModel
= DartsLinearRegressionModel(
dartslr =6,
lags=4,
output_chunk_length={
kwargs"fit_intercept": True,
},
)= ForecastingHorizon([1, 2, 3, 4], is_relative=True)
fh
# DartsRegressionModel
= DartsRegressionModel(
dartsr =6,
lags=4,
output_chunk_length=LinearRegression(),
model
)
# DartsXGBModel
= DartsXGBModel(
dartsxgb =6,
lags=4,
output_chunk_length={
kwargs"n_estimators": 100,
"max_depth": 3,
"objective": "reg:squarederror",
"eval_metric": "mae",
}, )
Fitting models to load_longley dataset
=fh)
dartslr.fit(y_train, fh=fh)
dartsr.fit(y_train, fh=fh) dartsxgb.fit(y_train, fh
Support for Torch based models not available. To enable them, install "darts", "u8darts[torch]" or "u8darts[all]" (with pip); or "u8darts-torch" or "u8darts-all" (with conda).
DartsXGBModel(kwargs={'eval_metric': 'mae', 'max_depth': 3, 'n_estimators': 100, 'objective': 'reg:squarederror'}, lags=6, output_chunk_length=4)Please rerun this cell to show the HTML repr or trust the notebook.
DartsXGBModel(kwargs={'eval_metric': 'mae', 'max_depth': 3, 'n_estimators': 100, 'objective': 'reg:squarederror'}, lags=6, output_chunk_length=4)
Fitting on darts.datasets Weather dataset
= WeatherDataset().load()
series = series["p (mbar)"][:100]
target = target.pd_series()
target_df
= copy.deepcopy(dartslr)
darts_weather_lr = copy.deepcopy(dartsr)
darts_weather_r = copy.deepcopy(dartsxgb) darts_weather_xgb
darts_weather_lr.fit(target_df)
darts_weather_r.fit(target_df) darts_weather_xgb.fit(target_df)
DartsXGBModel(kwargs={'eval_metric': 'mae', 'max_depth': 3, 'n_estimators': 100, 'objective': 'reg:squarederror'}, lags=6, output_chunk_length=4)Please rerun this cell to show the HTML repr or trust the notebook.
DartsXGBModel(kwargs={'eval_metric': 'mae', 'max_depth': 3, 'n_estimators': 100, 'objective': 'reg:squarederror'}, lags=6, output_chunk_length=4)
Making predictions
Making predictions on load_longley dataset
= ForecastingHorizon([1, 2], is_relative=True)
pred_fh = dartslr.predict(fh=pred_fh)
preds_dartslr = dartsr.predict(fh=pred_fh)
preds_dartsr = dartsxgb.predict(fh=pred_fh) preds_dartsxgb
print("DartsLinearRegressionModel predictions\n")
print(preds_dartslr)
print("\nDartsRegressionModel predictions\n")
print(preds_dartsr)
print("\nDartsXGBModel predictions\n")
print(preds_dartsxgb)
DartsLinearRegressionModel predictions
1959 65651.834793
1960 75258.144718
Freq: Y-DEC, Name: TOTEMP, dtype: float64
DartsRegressionModel predictions
1959 65651.834793
1960 75258.144718
Freq: Y-DEC, Name: TOTEMP, dtype: float64
DartsXGBModel predictions
1959 63761.011719
1960 66019.007812
Freq: Y-DEC, Name: TOTEMP, dtype: float32
Making predictions on darts.datasets Weather dataset
= list(range(1, 5))
pred_fh = darts_weather_lr.predict(fh=pred_fh)
preds_weather_darts_lr = darts_weather_r.predict(fh=pred_fh)
preds_weather_darts_r = darts_weather_xgb.predict(fh=pred_fh) preds_weather_darts_xgb
print("DartsLinearRegressionModel predictions\n")
print(preds_weather_darts_lr)
print("\nDartsRegressionModel predictions\n")
print(preds_weather_darts_r)
print("\nDartsXGBModel predictions\n")
print(preds_weather_darts_xgb)
DartsLinearRegressionModel predictions
2020-01-01 16:50:00 1005.729880
2020-01-01 17:00:00 1005.688367
2020-01-01 17:10:00 1005.649921
2020-01-01 17:20:00 1005.640799
Freq: 10min, Name: p (mbar), dtype: float64
DartsRegressionModel predictions
2020-01-01 16:50:00 1005.729880
2020-01-01 17:00:00 1005.688367
2020-01-01 17:10:00 1005.649921
2020-01-01 17:20:00 1005.640799
Freq: 10min, Name: p (mbar), dtype: float64
DartsXGBModel predictions
2020-01-01 16:50:00 1005.803833
2020-01-01 17:00:00 1005.724243
2020-01-01 17:10:00 1005.716980
2020-01-01 17:20:00 1005.714783
Freq: 10min, Name: p (mbar), dtype: float32
Conclusion
In this notebook, we demonstrate how you can simply perform time series forecasting using the darts
models through the sktime
interface. We have shown how to fit the models on the load_longley
and darts.datasets
Weather dataset and make predictions. For more details on sktime interface to darts, please refer to the sktime darts adapters documentation.