I am working with 2D arrays with the shape (1542, 2):
[[-0.83776179 -1. ]
[-0.83229744 -0.99845917]
[-0.81120124 -0.99691834]
...
[ 0.33190834 0.99691834]
[ 0.3312287 0.99845917]
[ 0.34686055 1. ]]
With this array, I need to group them so that for each entry, i have the 200 entries before, as I want to predict the next entrie using the 200 ones before, in this way:
[[[-0.83776179 -1. ]
[-0.83229744 -0.99845917]
[-0.81120124 -0.99691834]
...200 elements
[ 0.33190834 0.99691834]
[ 0.3312287 0.99845917]
[ 0.34686055 1. ]]
[[-0.83776179 -1. ]
[-0.83229744 -0.99845917]
[-0.81120124 -0.99691834]
... 200 elements
[ 0.33190834 0.99691834]
[ 0.3312287 0.99845917]
[ 0.34686055 1. ]]]
What would be the best way using numpy?
CodePudding user response:
If want you want is select the 200 features before each element, for the elements of index 200 to 1541:
With X
the original data
X_2 = np.array([X[i-200: i] for i in range(200, len(X))])
Thus print(X_2)
outputs (1342, 200, 2)
Each element of X_2
is a pack of 200 features to predict the next one. Because we began at index 200 (to get 200 elements before), we only have 1342 resulting elements.
Note that this takes up a lot of useless memory as you copy overlapping values of the original array into each entry of the new one.