我目前正在浏览 numpy,并且 numpy 中有一个名为“strides”的主题。我明白它是什么。但它是如何工作的?我在网上没有找到任何有用的信息。谁能让我通俗地理解一下?
3 回答
numpy 数组的实际数据存储在称为数据缓冲区的同构且连续的内存块中。有关更多信息,请参阅NumPy 内部。使用(默认)行优先顺序,二维数组如下所示:
为了将多维数组的索引 i,j,k,... 映射到数据缓冲区中的位置(偏移量,以字节为单位),NumPy 使用了 strides 的概念。步幅是为了沿数组的每个方向/维度从一项到下一项而在内存中跳过的字节数。换句话说,它是每个维度的连续项目之间的字节分隔。
例如:
>>> a = np.arange(1,10).reshape(3,3)
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
这个二维数组有两个方向,axis-0(垂直向下跨行)和axis-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]
如此。这意味着水平方向(轴-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]
。因此,垂直方向(axis-0)的步幅数为 3*4 = 12 个字节。请注意,从 开始a[0, 2] -> a[1, 0]
,通常是从第 i 行的最后一项到第 (i+1) 行的第一项,也是 4 个字节,因为数组a
是按行优先顺序存储的。
这就是为什么
>>> a.strides # (strides[0], strides[1])
(12, 4)
这是另一个示例,显示 2D 数组的水平方向(轴 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
as |1|2|3|4|5|6|7|8|9|
。使它们看起来相似的是不同的步幅。也就是说,字节步长为b[0, 0] -> b[0, 1]
3*4=12 字节,forb[0, 0] -> b[1, 0]
为 4 字节,而 fora[0, 0] -> a[0, 1]
为 4 字节,fora[0, 0] -> a[1, 0]
为 12 字节。
最后但同样重要的是,NumPy 允许使用修改步幅和形状的选项创建现有数组的视图,请参阅步幅技巧。例如:
>>> 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
只是为了补充@AndyK 的好答案,我从Numpy MedKit 了解了 numpy strides。在那里,他们显示了以下问题的使用:
给定输入:
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 - 数组沿每个轴的尺寸。
strides - 沿着特定维度前进到下一个项目必须跳过的内存字节数。
>>> 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或numpy.vstack并相信我,它在计算上要快得多。
笔记:
当使用这个技巧使用非常大的数组时,计算确切的步幅并不是那么简单。我通常制作一个numpy.zeroes
所需形状的数组,并array.strides
在函数中使用和使用它stride_tricks.as_strided
。
希望能帮助到你!
我已经修改了@Rick M. 提出的工作以适应我的问题,即移动任何形状的 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]])
2D 数组 - 通过多个切片进行切片
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]]])
2D 数组 - 通过每个切片的多个元素进行切片
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]]])