i am sorry for the very naive question.
I have these lines of numpy code, with a transpose at the beginning, and strides and reshape are used afterward.
I wonder if by re-ordering indices in reshape and stride, I could get rid of the first transpose function?
Please, can someone help me with that?
import numpy as np
# Input data
array = np.array([[1, 60],
[2, 70],
[3, 80],
[4, 90]])
depth = 3
# Function
n_rows, n_cols = array.shape
array = array.T # transpose function here
shape = array.shape[:-1] + (array.shape[-1] - depth + 1, depth - 1)
strides = array.strides + (array.strides[-1],)
array = np.lib.stride_tricks.as_strided(array, shape=shape, strides=strides)
reshaped = np.reshape(
np.swapaxes(array, 0, 1),
(n_rows-depth+1, (depth - 1) * n_cols),
)
Input and result are then:
array
Out[5]:
array([[ 1, 60],
[ 2, 70],
[ 3, 80],
[ 4, 90]])
reshaped
Out[6]:
array([[ 1, 2, 60, 70],
[ 2, 3, 70, 80]])