numpy中视图的ndarray.data行为 [英] Behavior of ndarray.data for views in numpy

查看:157
本文介绍了numpy中视图的ndarray.data行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解numpy中ndarray.data字段的含义(请参见内存布局部分,尤其是数组视图.引用文档:

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 into arrays. To quote the documentation:

ndarray.data-指向数组数据开头的Python缓冲区对象

ndarray.data -- Python buffer object pointing to the start of the array’s data

根据此描述,我希望它是指向ndarray实例基础的C数组的指针.

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

使用切片:y = x[3:1:-1]y形成为x的视图.

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

我期望x.data指向0.的位置,而y.data指向3.的位置.我期望由y.data打印的内存指针因此与由x.data打印的内存指针偏移3*x.itemsize个字节,但事实并非如此:

I was expecting x.data to point at location of 0. and y.data to point at the location of 3.. I was expecting the memory pointer printed by y.data to thus be offset by 3*x.itemsize bytes from the memory pointer printed by x.data, 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]
>>> x.data
<memory at 0x000000F2F5150348>
>>> y.data
<memory at 0x000000F2F5150408>
>>> int('0x000000F2F5150408', 16) - int('0x000000F2F5150348', 16)
192
>>> 3*x.itemsize
24

与ndarray实例关联的__array_interface词典中的'data'键的行为更像我期望的那样,尽管它本身可能不是指针:

The 'data' key in __array_interface 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]
24

所以这引出了一个问题,ndarray.data给出了什么?

谢谢.

推荐答案

通常,x.data显示的数字并不是您要使用的数字. x.data是缓冲区,可以在需要缓冲区的其他上下文中使用.

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)

复制您的x.

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

这将复制x[3:].但是从Python中,您不能使用x.data,向其添加192位(3 * 8 * 8),并希望获得x[3:].

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:].

我经常使用__array_interface__['data']值来检查两个变量是否共享一个数据缓冲区,但是我不会在任何事情上使用该数字.这些是提供信息的数字,而不是有效值.

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.

我最近在

直接从__array_interface__创建NumPy数组

这篇关于numpy中视图的ndarray.data行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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