gpt4 book ai didi

python - 多个维度的 Numpy 滚动

转载 作者:太空狗 更新时间:2023-10-29 20:58:35 25 4
gpt4 key购买 nike

我需要通过算法的 3D 位移向量移动 3D 数组。截至目前,我正在使用这种(公认的非常丑陋的)方法:

shiftedArray = np.roll(np.roll(np.roll(arrayToShift, shift[0], axis=0)
, shift[1], axis=1),
shift[2], axis=2)

这行得通,但意味着我要打 3 卷! (根据我的分析,我 58% 的算法时间花在了这些上)

来自 Numpy.roll 的文档:

Parameters:
shift : int

axis : int, optional

参数中没有提到类数组...所以我不能进行多维滚动?

我以为我可以调用这种函数(听起来像 Numpy 做的事):

np.roll(arrayToShift,3DshiftVector,axis=(0,1,2))

也许我的数组被 reshape 了一个扁平化的版本?但是我该如何计算移位向量呢?这种转变真的是一样的吗?

我很惊讶找不到简单的解决方案,因为我认为这是一件很常见的事情(好吧,不是常见的,但是......)

那么我们如何——相对地——有效地将一个 ndarray 移动一个 N 维向量?


注意:这个问题是在 2015 年提出的,当时 numpy 的滚动方法不支持此功能

最佳答案

理论上,按照@Ed Smith 的描述使用scipy.ndimage.interpolation.shift 应该可行,但由于一个错误(https://github.com/scipy/scipy/issues/1323),它没有给出这样的结果相当于多次调用np.roll


更新:“多卷”功能已添加到 numpy.roll在 numpy 版本 1.12.0 中。这是一个二维示例,其中第一个轴滚动一个位置,第二个轴滚动三个位置:

In [7]: x = np.arange(20).reshape(4,5)

In [8]: x
Out[8]:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])

In [9]: numpy.roll(x, [1, 3], axis=(0, 1))
Out[9]:
array([[17, 18, 19, 15, 16],
[ 2, 3, 4, 0, 1],
[ 7, 8, 9, 5, 6],
[12, 13, 14, 10, 11]])

这使得下面的代码过时了。我会把它留在那里供后代使用。


下面的代码定义了一个我称之为multiroll 的函数,它可以执行您想要的操作。这是一个将它应用于形状为 (500, 500, 500) 的数组的示例:

In [64]: x = np.random.randn(500, 500, 500)

In [65]: shift = [10, 15, 20]

多次调用 np.roll 生成预期结果:

In [66]: yroll3 = np.roll(np.roll(np.roll(x, shift[0], axis=0), shift[1], axis=1), shift[2], axis=2)

使用multiroll生成移位数组:

In [67]: ymulti = multiroll(x, shift)

验证我们得到了预期的结果:

In [68]: np.all(yroll3 == ymulti)
Out[68]: True

对于这样大小的数组,对 np.roll 进行三次调用几乎比对 multiroll 的调用慢三倍:

In [69]: %timeit yroll3 = np.roll(np.roll(np.roll(x, shift[0], axis=0), shift[1], axis=1), shift[2], axis=2)
1 loops, best of 3: 1.34 s per loop

In [70]: %timeit ymulti = multiroll(x, shift)
1 loops, best of 3: 474 ms per loop

下面是multiroll的定义:

from itertools import product
import numpy as np


def multiroll(x, shift, axis=None):
"""Roll an array along each axis.

Parameters
----------
x : array_like
Array to be rolled.
shift : sequence of int
Number of indices by which to shift each axis.
axis : sequence of int, optional
The axes to be rolled. If not given, all axes is assumed, and
len(shift) must equal the number of dimensions of x.

Returns
-------
y : numpy array, with the same type and size as x
The rolled array.

Notes
-----
The length of x along each axis must be positive. The function
does not handle arrays that have axes with length 0.

See Also
--------
numpy.roll

Example
-------
Here's a two-dimensional array:

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

Roll the first axis one step and the second axis three steps:

>>> multiroll(x, [1, 3])
array([[17, 18, 19, 15, 16],
[ 2, 3, 4, 0, 1],
[ 7, 8, 9, 5, 6],
[12, 13, 14, 10, 11]])

That's equivalent to:

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

Not all the axes must be rolled. The following uses
the `axis` argument to roll just the second axis:

>>> multiroll(x, [2], axis=[1])
array([[ 3, 4, 0, 1, 2],
[ 8, 9, 5, 6, 7],
[13, 14, 10, 11, 12],
[18, 19, 15, 16, 17]])

which is equivalent to:

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

"""
x = np.asarray(x)
if axis is None:
if len(shift) != x.ndim:
raise ValueError("The array has %d axes, but len(shift) is only "
"%d. When 'axis' is not given, a shift must be "
"provided for all axes." % (x.ndim, len(shift)))
axis = range(x.ndim)
else:
# axis does not have to contain all the axes. Here we append the
# missing axes to axis, and for each missing axis, append 0 to shift.
missing_axes = set(range(x.ndim)) - set(axis)
num_missing = len(missing_axes)
axis = tuple(axis) + tuple(missing_axes)
shift = tuple(shift) + (0,)*num_missing

# Use mod to convert all shifts to be values between 0 and the length
# of the corresponding axis.
shift = [s % x.shape[ax] for s, ax in zip(shift, axis)]

# Reorder the values in shift to correspond to axes 0, 1, ..., x.ndim-1.
shift = np.take(shift, np.argsort(axis))

# Create the output array, and copy the shifted blocks from x to y.
y = np.empty_like(x)
src_slices = [(slice(n-shft, n), slice(0, n-shft))
for shft, n in zip(shift, x.shape)]
dst_slices = [(slice(0, shft), slice(shft, n))
for shft, n in zip(shift, x.shape)]
src_blks = product(*src_slices)
dst_blks = product(*dst_slices)
for src_blk, dst_blk in zip(src_blks, dst_blks):
y[dst_blk] = x[src_blk]

return y

关于python - 多个维度的 Numpy 滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30639656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com