python 多个切片_python – numpy:将多个切片组装成新数组

我有一个二维数组,我需要将部分(切片)提取到一个新数组中:

original= numpy.ndarray( shape=(4,4) )

slices= numpy.ndarray( shape=(0,2) )

for x in range(3):

slice= original[x:x+2,x:x+2]

slices=numpy.append(slices, slice,axis=0)

有没有更有效的方法来做到这一点(摆脱python循环)?

– – 编辑 – –

为了澄清,我问如何将任意(但类似)形状的2D切片从2D阵列的任意2D索引复制到另一个垂直堆叠 – 特别是沿对角线或2×2大小.

解决方法:

stride_tricks有一个漂亮的技巧,你可以在SO和其他方面找到具有不同通用性的滚动窗口函数(目前numpy本身没有),这里是根据你得到的版本量身定制的:

def rolling_window(arr, window):

"""Very basic multi dimensional rolling window. window should be the shape of

of the desired subarrays. Window is either a scalar or a tuple of same size

as `arr.shape`.

"""

shape = np.array(arr.shape*2)

strides = np.array(arr.strides*2)

window = np.asarray(window)

shape[arr.ndim:] = window # new dimensions size

shape[:arr.ndim] -= window - 1

if np.any(shape < 1):

raise ValueError('window size is too large')

return np.lib.stride_tricks.as_strided(arr, shape=shape, strides=strides)

# Now:

view = rolling_window(arr, 2)

view[0,0] # first slice in your loop

请注意,视图包含与原始数组相同的数据!这可能会导致意外结果.但是你似乎只想要对角线,你也可以用大步技巧来做到这一点,以确保你不要复制数据(下一版本将创建一个带有对角线的视图,旧版本总是一个副本):

diagonal = np.diagonal(view, axis1=0, axis2=1)

# unfortunatly now the first slice is diagonal[...,0], so just roll it to the start:

diagonal = np.rollaxis(diagonal, -1)

现在,对角线是您在for循环中创建的数组(如果您不想要视图,则在较新版本上添加.copy()).

编辑:由于切片数组是2D而不是3D因为你追加,所以这里缺少一个重塑:

slices = diagonal.reshape(-1,2)

如果你有这么小的数组,但它的常量(期望在对角线调用中复制数据)与数组大小相比,这可能不会更快.

标签:python,slice,numpy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值