Skip to content

Commit a2a1f4d

Browse files
add offset (#254)
1 parent 97ab03f commit a2a1f4d

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

mlprimitives/custom/timeseries_preprocessing.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def intervals_to_mask(index, intervals):
3838

3939

4040
def rolling_window_sequences(X, index, window_size, target_size, step_size, target_column,
41-
drop=None, drop_windows=False):
41+
offset=0, drop=None, drop_windows=False):
4242
"""Create rolling window sequences out of time series data.
4343
4444
The function creates an array of input sequences and an array of target sequences by rolling
@@ -58,6 +58,8 @@ def rolling_window_sequences(X, index, window_size, target_size, step_size, targ
5858
Indicating the number of steps to move the window forward each round.
5959
target_column (int):
6060
Indicating which column of X is the target.
61+
offset (int):
62+
Indicating the number of steps between the input and the target sequence.
6163
drop (ndarray or None or str or float or bool):
6264
Optional. Array of boolean values indicating which values of X are invalid, or value
6365
indicating which value should be dropped. If not given, `None` is used.
@@ -89,7 +91,7 @@ def rolling_window_sequences(X, index, window_size, target_size, step_size, targ
8991
drop = X == drop
9092

9193
start = 0
92-
max_start = len(X) - window_size - target_size + 1
94+
max_start = len(X) - window_size - target_size - offset + 1
9395
while start < max_start:
9496
end = start + window_size
9597

@@ -101,9 +103,9 @@ def rolling_window_sequences(X, index, window_size, target_size, step_size, targ
101103
continue
102104

103105
out_X.append(X[start:end])
104-
out_y.append(target[end:end + target_size])
106+
out_y.append(target[end + offset:end + offset + target_size])
105107
X_index.append(index[start])
106-
y_index.append(index[end])
108+
y_index.append(index[end + offset])
107109
start = start + step_size
108110

109111
return np.asarray(out_X), np.asarray(out_y), np.asarray(X_index), np.asarray(y_index)

mlprimitives/primitives/mlprimitives.custom.timeseries_preprocessing.rolling_window_sequences.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
"type": "str or int",
6767
"default": 1
6868
},
69+
"offset": {
70+
"type": "int",
71+
"default": 0
72+
},
6973
"drop_windows": {
7074
"type": "bool",
7175
"default": false

tests/custom/test_timeseries_preprocessing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ def test_exceed_index(self):
9292
class RollingWindowSequencesTest(TestCase):
9393

9494
def _run(self, X, index, expected_X, expected_y, expected_X_index, expected_y_index,
95-
window_size=2, target_size=1, step_size=1, target_column=0, drop=None,
95+
window_size=2, target_size=1, step_size=1, target_column=0, drop=None, offset=0,
9696
drop_windows=False):
9797
X, y, X_index, y_index = rolling_window_sequences(X, index, window_size, target_size,
98-
step_size, target_column, drop,
98+
step_size, target_column, offset, drop,
9999
drop_windows)
100100
assert_allclose(X.astype(float), expected_X)
101101
assert_allclose(y.astype(float), expected_y)

0 commit comments

Comments
 (0)