Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions mlprimitives/custom/timeseries_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def intervals_to_mask(index, intervals):


def rolling_window_sequences(X, index, window_size, target_size, step_size, target_column,
drop=None, drop_windows=False):
offset=0, drop=None, drop_windows=False):
"""Create rolling window sequences out of time series data.

The function creates an array of input sequences and an array of target sequences by rolling
Expand All @@ -58,6 +58,8 @@ def rolling_window_sequences(X, index, window_size, target_size, step_size, targ
Indicating the number of steps to move the window forward each round.
target_column (int):
Indicating which column of X is the target.
offset (int):
Indicating the number of steps between the input and the target sequence.
drop (ndarray or None or str or float or bool):
Optional. Array of boolean values indicating which values of X are invalid, or value
indicating which value should be dropped. If not given, `None` is used.
Expand Down Expand Up @@ -89,7 +91,7 @@ def rolling_window_sequences(X, index, window_size, target_size, step_size, targ
drop = X == drop

start = 0
max_start = len(X) - window_size - target_size + 1
max_start = len(X) - window_size - target_size - offset + 1
while start < max_start:
end = start + window_size

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

out_X.append(X[start:end])
out_y.append(target[end:end + target_size])
out_y.append(target[end + offset:end + offset + target_size])
X_index.append(index[start])
y_index.append(index[end])
y_index.append(index[end + offset])
start = start + step_size

return np.asarray(out_X), np.asarray(out_y), np.asarray(X_index), np.asarray(y_index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
"type": "str or int",
"default": 1
},
"offset": {
"type": "int",
"default": 0
},
"drop_windows": {
"type": "bool",
"default": false
Expand Down
4 changes: 2 additions & 2 deletions tests/custom/test_timeseries_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ def test_exceed_index(self):
class RollingWindowSequencesTest(TestCase):

def _run(self, X, index, expected_X, expected_y, expected_X_index, expected_y_index,
window_size=2, target_size=1, step_size=1, target_column=0, drop=None,
window_size=2, target_size=1, step_size=1, target_column=0, drop=None, offset=0,
drop_windows=False):
X, y, X_index, y_index = rolling_window_sequences(X, index, window_size, target_size,
step_size, target_column, drop,
step_size, target_column, offset, drop,
drop_windows)
assert_allclose(X.astype(float), expected_X)
assert_allclose(y.astype(float), expected_y)
Expand Down