Skip to main content

tspiral

A python package for time series forecasting with scikit-learn estimators.

tspiral is not a library that works as a wrapper for other tools and methods for time series forecasting. tspiral directly provides scikit-learn estimators for time series forecasting. It leverages the benefit of using scikit-learn syntax and components to easily access the open source ecosystem built on top of the scikit-learn community. It easily maps a complex time series forecasting problems into a tabular supervised regression task, solving it with a standard approach.

Overview

tspiral provides 4 optimized forecasting techniques:

  • Recursive Forecasting

Lagged target features are combined with exogenous regressors (if provided) and lagged exogenous features (if specified). A scikit-learn compatible regressor is fitted on the whole merged data. The fitted estimator is called iteratively to predict multiple steps ahead.

recursive-standard

Which in a compact way we can summarize in:

recursive-compact

  • Direct Forecasting

A scikit-learn compatible regressor is fitted on the lagged data for each time step to forecast.

direct

Which in a compact way we can summarize in:

direct-compact

It's also possible to mix recursive and direct forecasting by predicting directly some future horizons while using recursive on the remaining.

directmix-compact

  • Stacking Forecasting

Multiple recursive time series forecasters are fitted and combined on the final portion of the training data with a meta-learner.

stacked

  • Rectified Forecasting

Multiple direct time series forecasters are fitted and combined on the final portion of the training data with a meta-learner.

rectify

GLOBAL and MULTIVARIATE time series forecasting are natively supported for all the forecasting methods available. For GLOBAL forecasting, use the groups parameter to specify the column of the input data that contains the group identifiers. For MULTIVARIATE forecasting, pass a target with multiple columns when calling fit.

Installation

pip install --upgrade tspiral

The module depends only on NumPy, Pandas, and Scikit-Learn (>=0.24.2). Python 3.6 or above is supported.

Media

Usage

  • Recursive Forecasting
import numpy as np
from sklearn.linear_model import Ridge
from tspiral.forecasting import ForecastingCascade
timesteps = 400
e = np.random.normal(0,1, (timesteps,))
y = np.concatenate([
    2*np.sin(np.arange(timesteps)*(2*np.pi/24))+e,
    2*np.cos(np.arange(timesteps)*(2*np.pi/24))+e,
])
X = [[0]]*timesteps+[[1]]*timesteps 
model = ForecastingCascade(
    Ridge(),
    lags=range(1,24+1),
    groups=[0],
).fit(X, y)
forecasts = model.predict([[0]]*80+[[1]]*80)
  • Direct Forecasting
import numpy as np
from sklearn.linear_model import Ridge
from tspiral.forecasting import ForecastingChain
timesteps = 400
e = np.random.normal(0,1, (timesteps,))
y = np.concatenate([
    2*np.sin(np.arange(timesteps)*(2*np.pi/24))+e,
    2*np.cos(np.arange(timesteps)*(2*np.pi/24))+e,
])
X = [[0]]*timesteps+[[1]]*timesteps 
model = ForecastingChain(
    Ridge(),
    n_estimators=24,
    lags=range(1,24+1),
    groups=[0],
).fit(X, y)
forecasts = model.predict([[0]]*80+[[1]]*80)
  • Stacking Forecasting
import numpy as np
from sklearn.linear_model import Ridge
from sklearn.tree import DecisionTreeRegressor
from tspiral.forecasting import ForecastingStacked
timesteps = 400
e = np.random.normal(0,1, (timesteps,))
y = np.concatenate([
    2*np.sin(np.arange(timesteps)*(2*np.pi/24))+e,
    2*np.cos(np.arange(timesteps)*(2*np.pi/24))+e,
])
X = [[0]]*timesteps+[[1]]*timesteps 
model = ForecastingStacked(
    [Ridge(), DecisionTreeRegressor()],
    test_size=24*3,
    lags=range(1,24+1),
    groups=[0],
).fit(X, y)
forecasts = model.predict([[0]]*80+[[1]]*80)
  • Rectified Forecasting
import numpy as np
from sklearn.linear_model import Ridge
from sklearn.tree import DecisionTreeRegressor
from tspiral.forecasting import ForecastingRectified
timesteps = 400
e = np.random.normal(0,1, (timesteps,))
y = np.concatenate([
    2*np.sin(np.arange(timesteps)*(2*np.pi/24))+e,
    2*np.cos(np.arange(timesteps)*(2*np.pi/24))+e,
])
X = [[0]]*timesteps+[[1]]*timesteps  
model = ForecastingRectified(
    [Ridge(), DecisionTreeRegressor()],
    n_estimators=24*3,
    test_size=24*3,
    lags=range(1,24+1),
    groups=[0],
).fit(X, y)
forecasts = model.predict([[0]]*80+[[1]]*80)

More examples in the notebooks folder.

Metadata

Release files for tspiral 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tspiral 0.3.0
File Size Uploaded
tspiral-0.3.0.tar.gz 17.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tspiral 0.3.0
File Interpreter ABI Platform
tspiral-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.9 kB

Release files / tspiral-0.3.0.tar.gz

Download URL tspiral-0.3.0.tar.gz
Size 17.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e4b22cf0236d3fac6e71fd4a3e9deffe4db6b929a2c46019dd2ceff99fb7e9f8
BLAKE2b-256 checksum
How to use checksums
9558e267d32ed0f21b7730d4f44e2d5411db6ba272054b87a434d4325bf8b9aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / tspiral-0.3.0-py3-none-any.whl

Download URL tspiral-0.3.0-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b6c247133d1b5ba7c670ccb75e33a1651425c96ade12e7715008573e189f8947
BLAKE2b-256 checksum
How to use checksums
6c153abe3e2f00bb07973cece43cb4a092fa8e8fa05633dedc726e0eff0d1619
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page