我有一个这种形式的2-d numpy数组:
[[ 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.]]
我想构建一个数组视图,在移动窗口中对其元素进行分组(在我的示例中为4)。我的结果应该是(6, 4, 5)
形状,我可以按如下方式构建它:
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
我想避免重新分配,所以我想知道我是否可以构造一个视图来提供这个结果,例如as_strided。
非常欢迎对此过程的解释。
由于
答案 0 :(得分:3)
以下是请求np.lib.stride_tricks.as_strided
-
def strided_axis0(a, L):
# INPUTS :
# a is array
# L is length of array along axis=0 to be cut for forming each subarray
# Length of 3D output array along its axis=0
nd0 = a.shape[0] - L + 1
# Store shape and strides info
m,n = a.shape
s0,s1 = a.strides
# Finally use strides to get the 3D array view
return np.lib.stride_tricks.as_strided(a, shape=(nd0,L,n), strides=(s0,s0,s1))
示例运行 -
In [48]: X = np.arange(35).reshape(-1,5)
In [49]: X
Out[49]:
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]])
In [50]: strided_axis0(X, L=4)
Out[50]:
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]],
[[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29]],
[[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34]]])
答案 1 :(得分:0)
我写了这个函数:
import numpy as np
def indices_moving_window(arr, win):
win_h = win[0]
win_w = win[1]
fh = arr.shape[0] - win_h + 1 # Final height
fw = arr.shape[1] - win_w + 1 # Final width
# Generate indices needed to iterate through the array with the moving window
ir = np.repeat(np.arange(fh), win_w).reshape(1, -1, win_w)
ir = np.repeat(ir, win_h, axis=1).reshape(-1, win_h, win_w)
ir = np.add(ir, np.arange(win_h).reshape(-1, win_h, 1))
ir = np.repeat(ir, fw, axis=0).reshape(fh, fw, win_h, win_w)
ic = np.repeat(np.arange(fw), win_h).reshape(1, -1, win_h)
ic = np.repeat(ic, win_w, axis=1).reshape(-1, win_h, win_w)
ic = np.add(ic, np.arange(win_w))
ic = ic.reshape(-1, win_w)
ic = np.tile(ic, (fh, 1))
ic = ic.reshape(fh, fw, win_h, win_w)
return ir, ic # Return indices for rows and columns
示例:
arr = np.arange(1,21).reshape(4,5)
rows, cols = indices_moving_window(arr, (3,4))
print(arr)
print(arr[rows,cols])
输出:
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20]]
[[[[ 1 2 3 4]
[ 6 7 8 9]
[11 12 13 14]]
[[ 2 3 4 5]
[ 7 8 9 10]
[12 13 14 15]]]
[[[ 6 7 8 9]
[11 12 13 14]
[16 17 18 19]]
[[ 7 8 9 10]
[12 13 14 15]
[17 18 19 20]]]]