user40314 user40314 - 1 year ago 112
Python Question

Behavior of for views in numpy

I am trying to understand the meaning of
field in numpy (see memory layout section of the reference page on N-dimensional arrays), especially for views into arrays. To quote the documentation: -- Python buffer object pointing to the start of the array’s data

According to this description, I was expecting this to be a pointer to the C-array underlying the instance of ndarray.

x = np.arange(5, dtype=np.float64)

as a view into
using a slice:
y = x[3:1:-1]

I was expecting
to point at location of
to point at the location of
. I was expecting the memory pointer printed by
to thus be offset by
bytes from the memory pointer printed by
, but this does not appear to be the case:

>>> import numpy as np
>>> x = np.arange(5, dtype=np.float64)
>>> y = x[ 3:1:-1]
<memory at 0x000000F2F5150348>
<memory at 0x000000F2F5150408>
>>> int('0x000000F2F5150408', 16) - int('0x000000F2F5150348', 16)
>>> 3*x.itemsize

key in
dictionary associated with the ndarray instance behaves more like I expect, although it may itself not be a pointer:

>>> y.__array_interface__['data'][0] - x.__array_interface__['data'][0]

So this begs the question, what does the

Thanks in advance.

Answer Source

Generally the number displayed by isn't meant to be used by you. is the buffer, which can be used in other contexts that expect a buffer.


replicates your x.


this replicates x[3:]. But from Python you can't take, add 192 bits (3*8*8) to it, and expect to get x[3:].

I often use the __array_interface__['data'] value to check whether two variables share a data buffer, but I don't use that number for any thing. These are informative numbers, not working values.

I recently explored this in

Creating a NumPy array directly from __array_interface__

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download