I have to reshape a ndarray of [17205, 21] as [17011, 96, 100, 21] by applying two sliding windows to it.
In: arr
Out: [[ 8. 0. 0. -0. 0. 0. 8. 8. 0. 0. 0. 0. 8. 7. 6. 9. 9. 1.
1. 1. 2.]
[ 8. 0. 0. -0. 0. 0. 8. 8. 0. 0. 0. 0. 8. 7. 5. 9. 8. 2.
1. 1. 2.]
.
.
.
[ 8. 0. 0. -0. 0. 0. 8. 8. 0. 0. 0. 0. 8. 7. 5. 9. 8. 3.
1. 1. 2.]]
My solution was to apply sliding windows to it two times. Then I apply the following method two times:
def separate_multi(sequences, n_steps):
X = list()
for i in range(len(sequences)):
# find the end of this pattern
end_ix = i + n_steps
# check if we are beyond the dataset
if end_ix > len(sequences):
break
# gather input and output parts of the pattern
seq_x = sequences[i:end_ix, :]
X.append(seq_x)
return np.array(X)
Giving the shape of [17106, 100, 21]
and then once again with n_step=96
, giving the shape of [17011, 96, 100, 21]
.
DRAWBACK: It stores the whole data in the memory which gives an error:
MemoryError: Unable to allocate 24.3 GiB for an array with shape (17011, 96, 100, 20) and data type float64
A possible solution:
import tensorflow as tf
df = tf.data.Dataset.from_tensor_slices(df)
df = df.window(100, shift=1, stride=1, drop_remainder=True)
df = df.window(96, shift=1, stride=1, drop_remainder=True)
However, it doesn't give me the desired output since "it produces a dataset of nested windows", as it is said here.
Any idea? Thanks