关于 numpy strides 的困惑 [英] Confusion about numpy strides

查看:92
本文介绍了关于 numpy strides 的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看这个问题的答案,但无法理解 as_strided 函数正在查看这个数组.

这段代码是答案的一部分:

>>>a = np.lib.stride_tricks.as_strided(np.array([1, 512, 0, 3], dtype=np.int16),形状=(3,),步幅=(3,))>>>一种数组([1, 2, 3], dtype=int16)>>>a.步幅[0]3>>>a.物品尺寸2

假设传递的数组的每个元素的长度为 2 个字节,我们有以下数组的字节表示:

-------------------------------------------------------------------------------------1 |第512话0 |3-----------------------------------------------------------------------0000 0000 0000 0001 |0000 0010 0000 0000 |0000 0000 0000 0000 |0000 0000 0000 0011

因此,考虑到每个要读取的元素为 2 个字节,到达下一个元素的步幅为 3 个字节:

  1. 读取的第一个元素是 1 (0000 0000 0000 0001),
  2. 要读取的第二个元素是跳过 3 个字节后出来的是 0 (0000 0000 | 0000 0000),一半是数字的字节512 和另一半从数字 0
  3. 在另一个 3 个字节的跨步之后读取的最后一个元素是 3:0000 0000 0000 0011

那么,我哪里出错了?如何在跨步输出中的中间元素 2 而不是 0

解决方案

np.array([1, 512, 0, 3], dtype=np.int16)的little-endian内存布局 实际上在内存中看起来是这样的(由于是小端,各个入口字节实际上与您编写它们的顺序相反):

(值)---------------------------------------------------------------------------------1 |第512话0 |3---------------------------------------------------------------------------------0000 0001 0000 0000 |0000 0000 0000 0010 |0000 0000 0000 0000 |0000 0011 0000 0000---------------------------------------------------------------------------------0 1 2 3 4 5 6 7(字节数)

stride=3 表示在items之间移动3个字节,所以你会得到字节数0-13-46-7.

这些是 0000 0001 0000 00000000 0010 0000 00000000 0011 0000 0000,再次解释为小端.><块引用>

数组的步幅告诉我们必须在内存中跳过多少字节才能沿某个轴移动到下一个位置.

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ndarray.strides.html

I am looking at the answer to this question and can't wrap my head around how the as_strided function is viewing this array.

This piece of code is part of the answer:

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

Assuming each element of the passed array is 2 bytes long, we have the following byte representation of the array:

-------------------------------------------------------------------------------------
         1          |        512          |        0            |          3
-------------------------------------------------------------------------------------
0000 0000 0000 0001 | 0000 0010 0000 0000 | 0000 0000 0000 0000 | 0000 0000 0000 0011

So, considering each element to be read is of 2 bytes and the stride to reach the next element is 3 bytes:

  1. the first element read is 1 (0000 0000 0000 0001),
  2. the second element to be read is after skipping 3 bytes comes out to be 0 (0000 0000 | 0000 0000), half are bytes from the number 512 and the other half from the number 0
  3. the last element to be read after another stride of 3 bytes is 3: 0000 0000 0000 0011

So, where am I going wrong? how is the middle element 2 in the strided output and not 0

解决方案

The little-endian memory layout of np.array([1, 512, 0, 3], dtype=np.int16) actually looks like this in memory (due to being little-endian, the individual entry bytes are actually in the reverse order from how you would write them):

(value)
-----------------------------------------------------------------------------------------
         1           |        512           |         0            |          3
-----------------------------------------------------------------------------------------
0000 0001  0000 0000 | 0000 0000  0000 0010 | 0000 0000  0000 0000 | 0000 0011  0000 0000
-----------------------------------------------------------------------------------------
        0          1           2          3           4          5           6          7
(byte number)

stride=3 means to move 3 bytes between items, so you'll get byte numbers 0-1, 3-4, 6-7.

These are 0000 0001 0000 0000, 0000 0010 0000 0000, 0000 0011 0000 0000, again interpreted as little-endian.

The strides of an array tell us how many bytes we have to skip in memory to move to the next position along a certain axis.

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ndarray.strides.html

这篇关于关于 numpy strides 的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆