python - Numpy rolling window over 2D array, as a 1D array with nested array as data values -
when using np.lib.stride_tricks.as_strided, how can manage 2d array nested arrays data values? there preferable efficient approach?
specifically, if have 2d np.array looking follows, each data item in 1d array array of length 2:
[[1., 2.],[3., 4.],[5.,6.],[7.,8.],[9.,10.]...] i want reshape rolling on follows:
[[[1., 2.],[3., 4.],[5.,6.]], [[3., 4.],[5.,6.],[7.,8.]], [[5.,6.],[7.,8.],[9.,10.]], ... ] i have had @ similar answers (e.g. this rolling window function), in use cannot leave inner array/tuples untouched.
for example window length of 3: have tried shape of (len(seq)+3-1, 3, 2) , stride of (2 * 8, 2 * 8, 8), no luck. maybe missing obvious?
cheers.
edit: easy produce functionally identical solution using python built-ins (which can optimised using e.g. np.arange similar divakar's solution), however, using as_strided? understanding, used highly efficient solution?
what wrong as_strided trial? works me.
in [28]: x=np.arange(1,11.).reshape(5,2) in [29]: x.shape out[29]: (5, 2) in [30]: x.strides out[30]: (16, 8) in [31]: np.lib.stride_tricks.as_strided(x,shape=(3,3,2),strides=(16,16,8)) out[31]: array([[[ 1., 2.], [ 3., 4.], [ 5., 6.]], [[ 3., 4.], [ 5., 6.], [ 7., 8.]], [[ 5., 6.], [ 7., 8.], [ 9., 10.]]]) on first edit used int array, had use (8,8,4) strides.
your shape wrong. if large starts seeing values off end of data buffer.
[[ 7.00000000e+000, 8.00000000e+000], [ 9.00000000e+000, 1.00000000e+001], [ 8.19968827e-257, 5.30498948e-313]]]) here alters display method, 7, 8, 9, 10 still there. writing those slots dangerous, messing other parts of code. as_strided best if used read-only purposes. writes/sets trickier.
Comments
Post a Comment