And*_*dyK 30
numpy数组的实际数据存储在称为数据缓冲区的同类且连续的内存块中.有关更多信息,请参阅NumPy内部.使用(默认)行主要顺序,2D数组如下所示:
为了将多维数组的索引i,j,k,...映射到数据缓冲区中的位置(偏移量,以字节为单位),NumPy使用步幅的概念.Strides是在内存中跳转的字节数,以便沿阵列的每个方向/维度从一个项目到下一个项目.换句话说,它是每个维度的连续项之间的字节分隔.
例如:
>>> a = np.arange(1,10).reshape(3,3)
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Run Code Online (Sandbox Code Playgroud)
这个2D数组有两个方向,axis-0(垂直向下跨行运行)和axis-1(跨列水平运行),每个项目的大小:
>>> a.itemsize # in bytes
4
Run Code Online (Sandbox Code Playgroud)
因此,从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]
.因此,垂直方向(轴-0)的步幅数是3*4 = 12个字节.注意,从a[0, 2] -> a[1, 0]
第i行的最后一项到第(i + 1)行的第一项,并且通常也是4字节,因为该数组a
以行主顺序存储.
这就是为什么
>>> a.strides # (strides[0], strides[1])
(12, 4)
Run Code Online (Sandbox Code Playgroud)
这是另一个示例,显示strides[1]
2D数组的水平方向(轴-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)
Run Code Online (Sandbox Code Playgroud)
这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字节,for b[0, 0] -> b[1, 0]
为4字节,而for a[0, 0] -> a[0, 1]
为4字节,for a[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]])
Run Code Online (Sandbox Code Playgroud)
这相当于转置数组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
Run Code Online (Sandbox Code Playgroud)
只是为了增加@AndyK的答案,我从Numpy MedKit了解了numpy的进步。在那里,它们显示出有问题的用法,如下所示:
给定输入:
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]])
Run Code Online (Sandbox Code Playgroud)
预期产量:
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]]])
Run Code Online (Sandbox Code Playgroud)
为此,我们需要了解以下术语:
shape-数组沿每个轴的尺寸。
步幅 -必须跳过的内存字节数,才能沿着特定维度前进到下一项。
>>> x.strides
(20, 4)
>>> np.int32().itemsize
4
Run Code Online (Sandbox Code Playgroud)
现在,如果我们查看“ 预期输出”:
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]]])
Run Code Online (Sandbox Code Playgroud)
我们需要操纵数组的形状和步幅。输出形状必须为(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]]])
Run Code Online (Sandbox Code Playgroud)
一种替代方法是:
>>> 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]]])
Run Code Online (Sandbox Code Playgroud)
我经常使用此方法而不是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)
Run Code Online (Sandbox Code Playgroud)
此方法自动计算步幅,它适用于任何维度的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]])
Run Code Online (Sandbox Code Playgroud)
一维数组 - 通过每个切片的多个元素进行切片
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]])
Run Code Online (Sandbox Code Playgroud)
二维数组 - 通过多个切片进行切片
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]]])
Run Code Online (Sandbox Code Playgroud)
二维数组 - 通过每个切片的多个元素进行切片
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]]])
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2710 次 |
最近记录: |