-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path00_utils.qmd
283 lines (208 loc) · 6.77 KB
/
00_utils.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
---
description: Functions to support the use of `gingado`
output-file: utils.html
title: Utils
jupyter: python3
warning: false
---
## Support for model documentation
```{python}
#| echo: false
#| output: false
%load_ext autoreload
%autoreload 2
```
```{python}
#| echo: false
#| output: false
from utils import show_doc
from __future__ import annotations
import datetime
import os
import numpy as np
import pandas as pd
from gingado.utils import (
get_datetime,
load_SDMX_data,
read_attr,
Lag,
list_SDMX_sources,
list_all_dataflows,
get_timefeat,
TemporalFeatureTransformer
)
# Code below included to ensure compatibility with scikit-learn v1.1.x
from sklearn import set_config
set_config(display='text')
# Code below for reproducibility of random numbers
rng = np.random.default_rng(seed=42)
```
```{python}
#| output: asis
#| echo: false
show_doc(get_datetime)
```
```{python}
d = get_datetime()
assert isinstance(d, str)
assert len(d) > 0
```
```{python}
#| output: asis
#| echo: false
show_doc(read_attr)
```
Function `read_attr` helps gingado Documenters to read the object behind the scenes.
It collects the type of estimator, and any attributes resulting from fitting an object (in ie, those that end in "_" without being double underscores).
For example, the attributes of an untrained and a trained random forest are, in sequence:
```{python}
from sklearn.ensemble import RandomForestRegressor
```
```{python}
rf_unfit = RandomForestRegressor(n_estimators=3)
rf_fit = RandomForestRegressor(n_estimators=3)\
.fit([[1, 0], [0, 1]], [[0.5], [0.5]]) # random numbers
list(read_attr(rf_unfit)), list(read_attr(rf_fit))
```
## Support for time series
Objects of the class `Lag` are similar to `scikit-learn`'s transformers.
```{python}
#| output: asis
#| echo: false
show_doc(Lag)
```
```{python}
#| output: asis
#| echo: false
show_doc(Lag.fit)
```
```{python}
#| output: asis
#| echo: false
show_doc(Lag.transform)
```
```{python}
#| output: asis
#| echo: false
show_doc(Lag.fit_transform)
```
The code below demonstrates how `Lag` works in practice. Note in particular that, because `Lag` is a transformer, it can be used as part of a `scikit-learn`'s `Pipeline`.
```{python}
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
```
```{python}
randomX = np.random.rand(15, 2)
randomY = np.random.rand(15)
lags = 3
jump = 2
pipe = Pipeline([
('scaler', StandardScaler()),
('lagger', Lag(lags=lags, jump=jump, keep_contemporaneous_X=False))
]).fit_transform(randomX, randomY)
```
Below we confirm that the lagger removes the correct number of rows corresponding to the lagged observations:
```{python}
assert randomX.shape[0] - lags - jump == pipe.shape[0]
```
And because `Lag` is a transformer, its parameters (`lags` and `jump`) can be calibrated using hyperparameter tuning to achieve the best performance for a model.
## Support for data augmentation with SDMX
:::{.callout-note}
please note that working with SDMX may take some minutes depending on the amount of information you are downloading.
:::
```{python}
#| output: asis
#| echo: false
show_doc(list_SDMX_sources)
```
```{python}
sources = list_SDMX_sources()
print(sources)
assert len(sources) > 0
# all elements are of type 'str'
assert sum([isinstance(src, str) for src in sources]) == len(sources)
```
```{python}
#| output: asis
#| echo: false
show_doc(list_all_dataflows)
```
```{python}
dflows = list_all_dataflows(return_pandas=False)
assert isinstance(dflows, dict)
all_sources = list_SDMX_sources()
assert len([s for s in dflows.keys() if s in all_sources]) == len(dflows.keys())
```
`list_all_dataflows` returns by default a pandas Series, facilitating data discovery by users like so:
```{python}
dflows = list_all_dataflows(return_pandas=True)
assert type(dflows) == pd.core.series.Series
dflows
```
This format allows for more easily searching `dflows` by source:
```{python}
list_all_dataflows(codes_only=True, return_pandas=True)
```
```{python}
dflows['BIS']
```
Or the user can search dataflows by their human-readable name instead of their code. For example, this is one way to see if any dataflow has information on interest rates:
```{python}
dflows[dflows.str.contains('Interest rate', case=False)]
```
The function `load_SDMX_data` is a convenience function that downloads data from SDMX sources (and any specific dataflows passed as arguments) if they match the key and parameters set by the user.
```{python}
#| output: asis
#| echo: false
show_doc(load_SDMX_data)
```
```{python}
df = load_SDMX_data(sources={'ECB': 'CISS', 'BIS': 'WS_CBPOL_D'}, keys={'FREQ': 'D'}, params={'startPeriod': 2003})
assert type(df) == pd.DataFrame
assert df.shape[0] > 0
assert df.shape[1] > 0
```
## Temporal features
Temporal features, such as the day of the week, month, or hour, provide valuable information for time series data, helping to capture seasonality, trends, and cyclic patterns. These features are especially useful because they represent known future information that can enhance model predictions.
The gingado library offers the `get_timefeat` method to extract these features from a time series:
```{python}
#| output: asis
#| echo: false
show_doc(get_timefeat)
```
For instance, using daily data from a DataFrame:
```{python}
# Display the first few rows of the DataFrame
display(df.head())
# Extract temporal features for daily data
temporal = get_timefeat(df, freq="D", add_to_df=False)
display(temporal.head())
```
You can also integrate the temporal features directly into the original DataFrame by setting the `add_to_df` parameter to True:
```{python}
# Generate a sample DataFrame with a weekly index
df_weekly = pd.DataFrame(
data={"value": rng.normal(size=100)},
index=pd.date_range('2000-01-01', periods=100, freq='W-MON')
)
# Add temporal features to the weekly data
df_with_timefeat = get_timefeat(df_weekly, freq="W", add_to_df=True)
display(df_with_timefeat.head())
```
If you only need a subset of the temporal features, you can specify the desired feature names:
```{python}
# Generate a new DataFrame with a monthly index
df_monthly = pd.DataFrame(
data={"value": rng.normal(size=24)},
index=pd.date_range("2023-01-01", periods=24, freq='MS')
)
# Only select a subset of temporal features:
df_with_timefeat = get_timefeat(df_monthly, freq="MS", columns=["month_of_year", "quarter_of_year"])
display(df_with_timefeat.head())
```
In addition to `get_timefeat`, the gingado library provides the `TemporalFeatureTransformer` class, which can be used to transform a DataFrame with a temporal index into a DataFrame with additional features:
```{python}
temp_trf = TemporalFeatureTransformer(freq="W", features=["week_of_month", "week_of_year", "quarter_of_year"])
df_with_timefeat = temp_trf.fit_transform(df_weekly)
display(df_with_timefeat.head())
```