following the array I have. I have added my expected result that i am not getting with attached code.
p= array([[0.26650886, 0.6108316 , 0.87093688, 0.56106049],
[0.27189878, 0.60786972, 0.87653939, 0.54244087],
[0.27508257, 0.60678571, 0.87979568, 0.5297218 ],
[0.27582241, 0.60754711, 0.88034473, 0.51667662],
[0.27606467, 0.60711087, 0.8800212 , 0.51716336],
[0.27705633, 0.60654571, 0.88044624, 0.52474009],
[0.27909608, 0.60545549, 0.88164035, 0.52696207],
[0.28027486, 0.60447923, 0.8821804 , 0.51754806],
[0.27989394, 0.6036416 , 0.88188837, 0.50952766],
[0.27953247, 0.6015729 , 0.88151134, 0.51027505]])
Expected result i want, lets say window_size=4 I am not getting this using below code.
[[0.26650886, 0.6108316 , 0.87093688, 0.56106049],
[0.27189878, 0.60786972, 0.87653939, 0.54244087],
[0.27508257, 0.60678571, 0.87979568, 0.5297218 ],
[0.27582241, 0.60754711, 0.88034473, 0.51667662]
[0.27189878, 0.60786972, 0.87653939, 0.54244087],
[0.27508257, 0.60678571, 0.87979568, 0.5297218 ],
[0.27582241, 0.60754711, 0.88034473, 0.51667662],
[0.27606467, 0.60711087, 0.8800212 , 0.51716336]
[0.27508257, 0.60678571, 0.87979568, 0.5297218 ],
[0.27582241, 0.60754711, 0.88034473, 0.51667662],
[0.27606467, 0.60711087, 0.8800212 , 0.51716336],
[0.27705633, 0.60654571, 0.88044624, 0.52474009].....]
I did the following code but it not giving me a result that I want.
# df is dataframe with shape(14, 4)
X = np.zeros(shape=(df.shape[0]-window_size,window_size,df.shape[1]))
for i in range(window_size-1, 10):
for j in range(i-window_size 1, i 1):
X[i-window_size 1][window_size-1-i j] = p[j]
Thanks in advance
CodePudding user response:
A more efficient solution would be to use sliding_window_view
from numpy.lib.stride_tricks
:
from numpy.lib.stride_tricks import sliding_window_view
values_per_arr = 4 # or p.shape[1]
window_size = 4
X = sliding_window_view(p, window_shape=(values_per_arr, window_size)).reshape(-1, values_per_arr, window_size)
Output:
>>> X
array([[[0.26650886, 0.6108316 , 0.87093688, 0.56106049],
[0.27189878, 0.60786972, 0.87653939, 0.54244087],
[0.27508257, 0.60678571, 0.87979568, 0.5297218 ],
[0.27582241, 0.60754711, 0.88034473, 0.51667662]],
[[0.27189878, 0.60786972, 0.87653939, 0.54244087],
[0.27508257, 0.60678571, 0.87979568, 0.5297218 ],
[0.27582241, 0.60754711, 0.88034473, 0.51667662],
[0.27606467, 0.60711087, 0.8800212 , 0.51716336]],
[[0.27508257, 0.60678571, 0.87979568, 0.5297218 ],
[0.27582241, 0.60754711, 0.88034473, 0.51667662],
[0.27606467, 0.60711087, 0.8800212 , 0.51716336],
[0.27705633, 0.60654571, 0.88044624, 0.52474009]],
[[0.27582241, 0.60754711, 0.88034473, 0.51667662],
[0.27606467, 0.60711087, 0.8800212 , 0.51716336],
[0.27705633, 0.60654571, 0.88044624, 0.52474009],
[0.27909608, 0.60545549, 0.88164035, 0.52696207]],
[[0.27606467, 0.60711087, 0.8800212 , 0.51716336],
[0.27705633, 0.60654571, 0.88044624, 0.52474009],
[0.27909608, 0.60545549, 0.88164035, 0.52696207],
[0.28027486, 0.60447923, 0.8821804 , 0.51754806]],
[[0.27705633, 0.60654571, 0.88044624, 0.52474009],
[0.27909608, 0.60545549, 0.88164035, 0.52696207],
[0.28027486, 0.60447923, 0.8821804 , 0.51754806],
[0.27989394, 0.6036416 , 0.88188837, 0.50952766]],
[[0.27909608, 0.60545549, 0.88164035, 0.52696207],
[0.28027486, 0.60447923, 0.8821804 , 0.51754806],
[0.27989394, 0.6036416 , 0.88188837, 0.50952766],
[0.27953247, 0.6015729 , 0.88151134, 0.51027505]]])
CodePudding user response:
Your code with window size of 4:
In [9]: arr =np.arange(4*14).reshape(14,4)
...: X = np.zeros(shape=(14-4,4,4),dtype=int)
...: for i in range(4-1,10):
...: for j in range(i-4 1,i 1):
...: X[i-4 1][4-1-i j] = arr[j] # arr instead of p
...:
In [10]: X
Out[10]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]],
[[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27]],
[[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31]],
[[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31],
[32, 33, 34, 35]],
[[24, 25, 26, 27],
[28, 29, 30, 31],
[32, 33, 34, 35],
[36, 37, 38, 39]],
[[ 0, 0, 0, 0],
[ 0, 0, 0, 0],
[ 0, 0, 0, 0],
[ 0, 0, 0, 0]],
[[ 0, 0, 0, 0],
[ 0, 0, 0, 0],
[ 0, 0, 0, 0],
[ 0, 0, 0, 0]],
[[ 0, 0, 0, 0],
[ 0, 0, 0, 0],
[ 0, 0, 0, 0],
[ 0, 0, 0, 0]]])
the source array:
In [11]: arr
Out[11]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31],
[32, 33, 34, 35],
[36, 37, 38, 39],
[40, 41, 42, 43],
[44, 45, 46, 47],
[48, 49, 50, 51],
[52, 53, 54, 55]])
Other than the fact that it didn't fill in the last 3 blocks, I don't see what's the problem.