如何理解外行的 numpy strides?

How to understand numpy strides for layman?

我目前正在研究 numpy,在 numpy 中有一个主题叫做 "strides"。我明白那是什么。但是它是如何工作的呢?我没有在网上找到任何有用的信息。谁能通俗的告诉我一下?

numpy 数组的实际数据存储在称为数据缓冲区的同类且连续的内存块中。有关详细信息,请参阅 NumPy internals。 使用(默认)row-major 顺序,二维数组如下所示:

为了将多维数组的索引 i,j,k,... 映射到数据缓冲区中的位置(偏移量,以字节为单位),NumPy 使用 strides。 步幅是在内存中跳过的字节数,以便沿着数组的每个 direction/dimension 从一个项目到达 下一个 项目。换句话说,它是每个维度的连续项目之间的字节分隔。

例如:

>>> a = np.arange(1,10).reshape(3,3)
>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

这个二维数组有两个方向,轴 0(运行 垂直向下跨行)和轴 1(运行 水平跨列),每个项目的大小为:

>>> a.itemsize  # in bytes
4  

所以从 a[0, 0] -> a[0, 1] 开始(沿第 0 行水平移动,从第 0 列到第 1 列)数据缓冲区中的字节步长为 4。与 a[0, 1] -> a[0, 2] 相同, a[1, 0] -> a[1, 1]等。这意味着水平方向(axis-1)的步幅数为4个字节。

但是,要从a[0, 0] -> a[1, 0]开始(沿第0列垂直移动,从第0行到第1行),需要先遍历第0行剩余的所有项目才能到达第 1 行,然后移动到第 1 行以到达项目 a[1, 0],即 a[0, 0] -> a[0, 1] -> a[0, 2] -> a[1, 0]。因此,垂直方向(轴 0)的步幅数为 3*4 = 12 个字节。请注意,从 a[0, 2] -> a[1, 0] 开始,通常从第 i 行的最后一项到第 (i+1) 行的第一项,也是 4 个字节,因为数组 a以行优先顺序存储。

这就是为什么

>>> a.strides  # (strides[0], strides[1])
(12, 4)  

这是另一个示例,显示二维数组在水平方向(轴 1)strides[1] 上的步幅不一定等于项目大小(例如,具有列优先顺序的数组) :

>>> b = np.array([[1, 4, 7],
                  [2, 5, 8],
                  [3, 6, 9]]).T
>>> b
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

>>> b.strides
(4, 12)

此处 strides[1] 是项目大小的倍数。尽管数组 b 看起来与数组 a 相同,但它是不同的数组:在内部 b 存储为 |1|4|7|2|5|8|3|6|9| (因为转置不影响数据缓冲区,但仅交换步幅和形状),而 a|1|2|3|4|5|6|7|8|9|。让他们看起来相似的是不同的步伐。也就是说,b[0, 0] -> b[0, 1] 的字节步长是 3*4=12 字节,b[0, 0] -> b[1, 0] 是 4 字节,而 a[0, 0] -> a[0, 1] 是 4 字节,a[0, 0] -> a[1, 0] 是 12字节。

最后但同样重要的是,NumPy 允许创建现有数组的视图,并可选择修改步幅和形状,请参阅 stride tricks。例如:

>>> np.lib.stride_tricks.as_strided(a, shape=a.shape[::-1], strides=a.strides[::-1])
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])

相当于转置数组a.

让我补充一点,但不涉及太多细节,甚至可以定义不是项目大小倍数的步幅。这是一个例子:

>>> a = np.lib.stride_tricks.as_strided(np.array([1, 512, 0, 3], dtype=np.int16), 
                                        shape=(3,), strides=(3,))
>>> a
array([1, 2, 3], dtype=int16)

>>> a.strides[0]
3

>>> a.itemsize
2

只是添加到很棒的 , I learnt about numpy strides from Numpy MedKit。他们在那里显示了以下问题的使用:

给定输入:

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]])

预期输出

array([[[  0,  1,  2,  3,  4],
        [  5,  6,  7,  8,  9]],

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

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

为此,我们需要了解以下术语:

shape - 数组沿每个轴的维度。

步幅 - 沿特定维度前进到下一个项目必须跳过的内存字节数。

>>> x.strides
(20, 4)

>>> np.int32().itemsize
4

现在,如果我们查看预期输出

array([[[  0,  1,  2,  3,  4],
        [  5,  6,  7,  8,  9]],

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

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

我们需要操纵数组的形状和步幅。输出形状必须是 (3, 2, 5),即 3 个项目,每个项目包含两行 (m == 2),每行有 5 个元素。

步幅需要从 (20, 4) 变为 (20, 20, 4)。新输出数组中的每一项都从一个新行开始,每行由 20 个字节组成(5 个元素,每个元素 4 个字节),每个元素占用 4 个字节(int32)。

所以:

>>> from numpy.lib import stride_tricks
>>> stride_tricks.as_strided(x, shape=(3, 2, 5),
                                strides=(20, 20, 4))
...
array([[[  0,  1,  2,  3,  4],
        [  5,  6,  7,  8,  9]],

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

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

另一种选择是:

>>> d = dict(x.__array_interface__)
>>> d['shape'] = (3, 2, 5)
>>> s['strides'] = (20, 20, 4)

>>> class Arr:
...     __array_interface__ = d
...     base = x

>>> np.array(Arr())
array([[[  0,  1,  2,  3,  4],
        [  5,  6,  7,  8,  9]],

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

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

我经常使用这种方法而不是 numpy.hstack or numpy.vstack,相信我,从计算上讲它要快得多。

注:

当使用这个技巧使用非常大的数组时,计算精确的 步幅 并不是那么简单。我通常制作一个所需形状的 numpy.zeroes 数组,并使用 array.strides 获得步幅,并在函数 stride_tricks.as_strided.

中使用它

希望对您有所帮助!

我已经改编了@Rick M. 提出的工作来解决我的问题,即移动 window 任意形状的 numpy 数组的切片。这是代码:

def sliding_window_slicing(a, no_items, item_type=0):
    """This method perfoms sliding window slicing of numpy arrays

    Parameters
    ----------
    a : numpy
        An array to be slided in subarrays
    no_items : int
        Number of sliced arrays or elements in sliced arrays
    item_type: int
        Indicates if no_items is number of sliced arrays (item_type=0) or
        number of elements in sliced array (item_type=1), by default 0

    Return
    ------
    numpy
        Sliced numpy array
    """
    if item_type == 0:
        no_slices = no_items
        no_elements = len(a) + 1 - no_slices
        if no_elements <=0:
            raise ValueError('Sliding slicing not possible, no_items is larger than ' + str(len(a)))
    else:
        no_elements = no_items                
        no_slices = len(a) - no_elements + 1
        if no_slices <=0:
            raise ValueError('Sliding slicing not possible, no_items is larger than ' + str(len(a)))

    subarray_shape = a.shape[1:]
    shape_cfg = (no_slices, no_elements) + subarray_shape
    strides_cfg = (a.strides[0],) + a.strides
    as_strided = np.lib.stride_tricks.as_strided #shorthand
    return as_strided(a, shape=shape_cfg, strides=strides_cfg)

此方法自动计算 步幅 并且适用于任何维度的 numpy 数组:

一维数组 - 通过多个切片进行切片

In [11]: a                                                                                                                                                     
Out[11]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [12]: sliding_window_slicing(a, 5, item_type=0)                                                                                                                          
Out[12]: 
array([[0, 1, 2, 3, 4, 5],
       [1, 2, 3, 4, 5, 6],
       [2, 3, 4, 5, 6, 7],
       [3, 4, 5, 6, 7, 8],
       [4, 5, 6, 7, 8, 9]])

一维数组 - 通过每个切片的多个元素切片

In [13]: sliding_window_slicing(a, 5, item_type=1)                                                                                                             
Out[13]: 
array([[0, 1, 2, 3, 4],
       [1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7],
       [4, 5, 6, 7, 8],
       [5, 6, 7, 8, 9]])

二维数组 - 通过多个切片进行切片

In [16]: a = np.arange(10).reshape([5,2])                                                                                                                      

In [17]: a                                                                                                                                                     
Out[17]: 
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])

In [18]: sliding_window_slicing(a, 2, item_type=0)                                                                                                             
Out[18]: 
array([[[0, 1],
        [2, 3],
        [4, 5],
        [6, 7]],

       [[2, 3],
        [4, 5],
        [6, 7],
        [8, 9]]])

二维数组 - 通过每个切片的多个元素切片

In [19]: sliding_window_slicing(a, 2, item_type=1)                                                                                                             
Out[19]: 
array([[[0, 1],
        [2, 3]],

       [[2, 3],
        [4, 5]],

       [[4, 5],
        [6, 7]],

       [[6, 7],
        [8, 9]]])