[英]How to efficiently slide numpy array?
I would like to slide a ND numpy array.我想滑动一个 ND numpy 数组。 Currently, the code below do the work.
目前,下面的代码完成了这项工作。
import numpy as np
arr = np.array([np.arange(0,16), np.arange(17,33),np.arange(33,49)])
window_size=4
expected_opt=[arr [:, i:i+window_size]for i in range(0,16,window_size)]
But I curious whether there is more efficient way to achieve similar objective.但我很好奇是否有更有效的方法来实现类似的目标。
one might suggest 1 , but the solution gives different output.有人可能会建议1 ,但解决方案给出了不同的输出。
You are looking at a reshape, not rolling:您正在寻找重塑,而不是滚动:
arr.reshape(arr.shape[0],-1,window_size).transpose(1,0,2)
Output:输出:
array([[[ 0, 1, 2, 3],
[17, 18, 19, 20],
[33, 34, 35, 36]],
[[ 4, 5, 6, 7],
[21, 22, 23, 24],
[37, 38, 39, 40]],
[[ 8, 9, 10, 11],
[25, 26, 27, 28],
[41, 42, 43, 44]],
[[12, 13, 14, 15],
[29, 30, 31, 32],
[45, 46, 47, 48]]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.