Skip to content

Commit cf7486b

Browse files
committed
refactor for natively support global forecasting
1 parent c6afa2b commit cf7486b

File tree

10 files changed

+1660
-5485
lines changed

10 files changed

+1660
-5485
lines changed

README.md

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ Multiple direct time series forecasters are fitted and combined on the final por
4343

4444
![rectify](https://raw.githubusercontent.com/cerlymarco/tspiral/master/imgs/rectify.PNG)
4545

46-
Multivariate time series forecasting is natively supported for all the forecasting methods available.
46+
**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.
4747

4848
## Installation
4949
```shell
5050
pip install --upgrade tspiral
5151
```
52-
The module depends only on NumPy and Scikit-Learn (>=0.24.2). Python 3.6 or above is supported.
52+
The module depends only on NumPy, Pandas, and Scikit-Learn (>=0.24.2). Python 3.6 or above is supported.
5353

5454
## Media
5555

@@ -68,15 +68,17 @@ from sklearn.linear_model import Ridge
6868
from tspiral.forecasting import ForecastingCascade
6969
timesteps = 400
7070
e = np.random.normal(0,1, (timesteps,))
71-
y = 2*np.sin(np.arange(timesteps)*(2*np.pi/24))+e
71+
y = np.concatenate([
72+
2*np.sin(np.arange(timesteps)*(2*np.pi/24))+e,
73+
2*np.cos(np.arange(timesteps)*(2*np.pi/24))+e,
74+
])
75+
X = [[0]]*timesteps+[[1]]*timesteps
7276
model = ForecastingCascade(
7377
Ridge(),
7478
lags=range(1,24+1),
75-
use_exog=False,
76-
accept_nan=False
77-
)
78-
model.fit(None, y)
79-
forecasts = model.predict(np.arange(24*3))
79+
groups=[0],
80+
).fit(X, y)
81+
forecasts = model.predict([[0]]*80+[[1]]*80)
8082
```
8183

8284
- **Direct Forecasting**
@@ -86,16 +88,18 @@ from sklearn.linear_model import Ridge
8688
from tspiral.forecasting import ForecastingChain
8789
timesteps = 400
8890
e = np.random.normal(0,1, (timesteps,))
89-
y = 2*np.sin(np.arange(timesteps)*(2*np.pi/24))+e
91+
y = np.concatenate([
92+
2*np.sin(np.arange(timesteps)*(2*np.pi/24))+e,
93+
2*np.cos(np.arange(timesteps)*(2*np.pi/24))+e,
94+
])
95+
X = [[0]]*timesteps+[[1]]*timesteps
9096
model = ForecastingChain(
9197
Ridge(),
9298
n_estimators=24,
9399
lags=range(1,24+1),
94-
use_exog=False,
95-
accept_nan=False
96-
)
97-
model.fit(None, y)
98-
forecasts = model.predict(np.arange(24*3))
100+
groups=[0],
101+
).fit(X, y)
102+
forecasts = model.predict([[0]]*80+[[1]]*80)
99103
```
100104

101105
- **Stacking Forecasting**
@@ -106,15 +110,18 @@ from sklearn.tree import DecisionTreeRegressor
106110
from tspiral.forecasting import ForecastingStacked
107111
timesteps = 400
108112
e = np.random.normal(0,1, (timesteps,))
109-
y = 2*np.sin(np.arange(timesteps)*(2*np.pi/24))+e
113+
y = np.concatenate([
114+
2*np.sin(np.arange(timesteps)*(2*np.pi/24))+e,
115+
2*np.cos(np.arange(timesteps)*(2*np.pi/24))+e,
116+
])
117+
X = [[0]]*timesteps+[[1]]*timesteps
110118
model = ForecastingStacked(
111119
[Ridge(), DecisionTreeRegressor()],
112120
test_size=24*3,
113121
lags=range(1,24+1),
114-
use_exog=False
115-
)
116-
model.fit(None, y)
117-
forecasts = model.predict(np.arange(24*3))
122+
groups=[0],
123+
).fit(X, y)
124+
forecasts = model.predict([[0]]*80+[[1]]*80)
118125
```
119126

120127
- **Rectified Forecasting**
@@ -125,16 +132,19 @@ from sklearn.tree import DecisionTreeRegressor
125132
from tspiral.forecasting import ForecastingRectified
126133
timesteps = 400
127134
e = np.random.normal(0,1, (timesteps,))
128-
y = 2*np.sin(np.arange(timesteps)*(2*np.pi/24))+e
135+
y = np.concatenate([
136+
2*np.sin(np.arange(timesteps)*(2*np.pi/24))+e,
137+
2*np.cos(np.arange(timesteps)*(2*np.pi/24))+e,
138+
])
139+
X = [[0]]*timesteps+[[1]]*timesteps
129140
model = ForecastingRectified(
130141
[Ridge(), DecisionTreeRegressor()],
131-
n_estimators=200,
142+
n_estimators=24*3,
132143
test_size=24*3,
133144
lags=range(1,24+1),
134-
use_exog=False
135-
)
136-
model.fit(None, y)
137-
forecasts = model.predict(np.arange(24*3))
145+
groups=[0],
146+
).fit(X, y)
147+
forecasts = model.predict([[0]]*80+[[1]]*80)
138148
```
139149

140150
More examples in the [notebooks folder](https://github.com/cerlymarco/tspiral/tree/main/notebooks).

notebooks/direct-forecasting.ipynb

Lines changed: 203 additions & 1547 deletions
Large diffs are not rendered by default.

notebooks/rectified-forecasting.ipynb

Lines changed: 207 additions & 1588 deletions
Large diffs are not rendered by default.

notebooks/recursive-forecasting.ipynb

Lines changed: 184 additions & 809 deletions
Large diffs are not rendered by default.

notebooks/stacked-forecasting.ipynb

Lines changed: 203 additions & 863 deletions
Large diffs are not rendered by default.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
numpy
2+
pandas
23
scipy
34
scikit-learn>=0.24.2

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
HERE = pathlib.Path(__file__).parent
55

6-
VERSION = '0.2.0'
6+
VERSION = '0.3.0'
77
PACKAGE_NAME = 'tspiral'
88
AUTHOR = 'Marco Cerliani'
99
AUTHOR_EMAIL = 'cerlymarco@gmail.com'
@@ -16,7 +16,8 @@
1616

1717
INSTALL_REQUIRES = [
1818
'scikit-learn>=0.24.2',
19-
'numpy'
19+
'numpy',
20+
'pandas',
2021
]
2122

2223
setup(name=PACKAGE_NAME,

tspiral/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
from .forecasting import *
2-
from .model_selection import *
1+
__version__ = "0.3.0"
2+
from .model_selection import TemporalSplit
3+
from .forecasting import ForecastingCascade, ForecastingChain
4+
from .forecasting import ForecastingStacked, ForecastingRectified

0 commit comments

Comments
 (0)