繁体   English   中英

使用重复行将2d NumPy数组重新整形为3d

[英]Reshaping 2d NumPy array into 3d with recurring rows

我有一个NumPy数组如下:

arr = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]])

我希望安排它看起来像这样:

[[[6,7,8,9,10],
  [1,2,3,4,5]],
 [[11,12,13,14,15],
  [6,7,8,9,10]],
 [[16,17,18,19,20],
  [11,12,13,14,15]]]

所以基本上是一个3D阵列,阵列的每一行都有2x5。 我试过的代码是:

x=np.zeros([3,2,5])
for i in range(len(arr)):
    x[i]=arr[i:i+2,:][::-1]

但这导致以下输出:

[[[ 6.  7.  8.  9. 10.]
  [ 1.  2.  3.  4.  5.]]    
 [[ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]]  
 [[ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]]]

[[[ 6.  7.  8.  9. 10.]
  [ 1.  2.  3.  4.  5.]]    
 [[11. 12. 13. 14. 15.]
  [ 6.  7.  8.  9. 10.]]    
 [[ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]]]

[[[ 6.  7.  8.  9. 10.]
  [ 1.  2.  3.  4.  5.]]    
 [[11. 12. 13. 14. 15.]
  [ 6.  7.  8.  9. 10.]]    
 [[16. 17. 18. 19. 20.]
  [11. 12. 13. 14. 15.]]]

您可以使用一些步幅技巧将数组构造为输入数组上的多维滑动窗口:

import numpy as np 
arr = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]]) 

# compute the strides and shape of the output array
in_strides = arr.strides 
out_strides = in_strides[:1] + in_strides 
out_shape = (3, 2) + arr.shape[-1:]  # keep the last dimension's size
strided = np.lib.stride_tricks.as_strided(arr, strides=out_strides, shape=out_shape)
out_arr = strided[:, ::-1, :].copy()  # use a copy to stay safe

只要out_shape[-1] <= arr.shape[1]sum(out_shape[:2]) <= arr.shape[0] + 1 ,上述操作就会安全有效。 这些约束使得滑动窗口在原始数组中有意义,并且您的实际用例应该自然地尊重这些。

重要笔记:

  • 如果上面的不等式不成立,那么滑动窗口将很乐意滑出数组的内存范围,你将默默地开始看到垃圾矩阵元素:

     >>> out_strides = in_strides[:1] + in_strides ... out_shape = (3, 3, 5) # 3 + 3 == 6 > arr.shape[0] + 1 == 5 ... np.lib.stride_tricks.as_strided(arr, strides=out_strides, shape=out_shape) array([[[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [ 11, 12, 13, 14, 15]], [[ 6, 7, 8, 9, 10], [ 11, 12, 13, 14, 15], [ 16, 17, 18, 19, 20]], [[ 11, 12, 13, 14, 15], [ 16, 17, 18, 19, 20], [ 384, 193, 94379169559968, 0, 0]]]) 
  • 如果之后你不会改变你的数组,那么你可以在上面省略最后的.copy()调用。 这将为您提供一个与原始数组共享内存的跨步数组,但更重要的是,数组的行将彼此共享内存。 不是您通常想要的,但如果您的真实数组非常大, 并且您知道可以安全地假设这些值不会被独立地变异,那么内存占用可能很重要。 另一个需要考虑的方面是,在结果上调用.copy()将为您提供一个连续的内存块,这可能会更好地实现性能,具体取决于您计划对结果数组执行的操作。

我们可以利用基于np.lib.stride_tricks.as_strided scikit-image's view_as_windows来获得滑动窗口。 有关使用基于view_as_windowsas_strided的更多信息

from skimage.util.shape import view_as_windows

x = view_as_windows(arr,(2,arr.shape[1]))[:,0,::-1]

这只是输入数组的视图。 因此,没有额外的内存开销和几乎免费的运行时。 如果你想要一个带有自己内存空间的输出,可以在那里附加.copy() ,即x.copy()

样品运行 -

In [15]: from skimage.util.shape import view_as_windows

In [16]: view_as_windows(arr,(2,arr.shape[1]))[:,0,::-1]
Out[16]: 
array([[[ 6,  7,  8,  9, 10],
        [ 1,  2,  3,  4,  5]],

       [[11, 12, 13, 14, 15],
        [ 6,  7,  8,  9, 10]],

       [[16, 17, 18, 19, 20],
        [11, 12, 13, 14, 15]]])

无需使用任何循环。 切片就足够了:

x = np.zeros([3,2,5], dtype=int)
x[:,0] = arr[-3:,:]
x[:,1] = arr[:3,:]

基本上你在所有页面的最后3行的第0行分配arr ,并在所有页第1行至第3行的arr

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM