Behavior of ndarray.data for views in numpy

后端 未结 2 942
慢半拍i
慢半拍i 2021-01-22 20:02

I am trying to understand the meaning of ndarray.data field in numpy (see memory layout section of the reference page on N-dimensional arrays), especially for views

相关标签:
2条回答
  • 2021-01-22 20:25

    <memory at 0x000000F2F5150348> is a memoryview object located at address 0x000000F2F5150348; the buffer it provides access to is located somewhere else.

    Memoryviews provide a number of operations described in the relevant official documentation, but at least on the Python-side API, they do not provide any way to access the raw address of the memory they expose. Particularly, the at whatevernumber number is not what you're looking for.

    0 讨论(0)
  • 2021-01-22 20:36

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

    np.frombuffer(x.data,dtype=float)
    

    replicates your x.

    np.frombuffer(x[3:].data,dtype=float)
    

    this replicates x[3:]. But from Python you can't take x.data, 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__

    0 讨论(0)
提交回复
热议问题