I have a 2-d numpy array of this form:
[[ 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.]]
I want to construct a view of the array, grouping its elements in a moving window (of size 4 in my example). My result should be of shape (6, 4, 5)
and I can construct it as follows:
res = []
mem = 4
for i in range(mem, X.shape[0]+1):
res.append(X[i-mem:i, : ])
res = np.asarray(res)
print res.shape
I want to avoid reallocation, so I wonder if I can construct a view to give this result, with as_strided for example.
An explanation of the process is very welcome.
Thanks