Numpy ,Python3.6 - 无法理解为什么地址不同? [英] Numpy , Python3.6 - not able to understand why address is different?

查看:51
本文介绍了Numpy ,Python3.6 - 无法理解为什么地址不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

my_array_1 = np.arange(25).reshape(5, 5)
print(my_array_1)

my_array_red = my_array_1[:, 1::2]
print(my_array_red)

my_array_blue = my_array_1[1::2, 0:3:2]
print(my_array_blue)

my_array_yellow = my_array_1[-1, :]
print(my_array_yellow)

print(id(my_array_1))
print(id(my_array_red))
print(id(my_array_yellow))
print(id(my_array_blue))

print(my_array_1.data)
print(my_array_red.data)
print(my_array_blue.data)
print(my_array_yellow.data)

这是输出:

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]
[[ 1  3]
 [ 6  8]
 [11 13]
 [16 18]
 [21 23]]
[[ 5  7]
 [15 17]]
[20 21 22 23 24]
2606769150592
2606769282544
2607017647120
2606769282624
<memory at 0x0000025EFE56CA68>
<memory at 0x0000025EFE56CA68>
<memory at 0x0000025EFE56CA68>
<memory at 0x0000025EFE5A8F48>

问题:只需检查我的输出的最后 4 行.为什么 my_array_1.data.data , my_array_red.data, my_array_blue.data 有相同的值,但是 my_array_yellow.data 有不同的值?

Question : Just check the last 4 lines of my output . why my_array_1.data.data , my_array_red.data, my_array_blue.data have same value , But where as my_array_yellow.data have a different value ?

推荐答案

我发现 __array_interface__data 值提供了更多信息:

I find the data value of the __array_interface__ to be more informative:

In [2]: my_array_1.__array_interface__['data']
Out[2]: (33691856, False)
In [3]: my_array_red.__array_interface__['data']
Out[3]: (33691864, False)
In [4]: my_array_blue.__array_interface__['data']
Out[4]: (33691896, False)
In [5]: my_array_yellow.__array_interface__['data']
Out[5]: (33692016, False)

Out[2] 是数据缓冲区的开始.

Out[2] is the start of the data buffer.

red 大 8 个字节 - 从一开始就是一个元素.

red is 8 bytes larger - that is one element from the start.

blue 是 40 字节 in - 下一行

blue is 40 bytes in - the next row

In [8]: my_array_1.strides
Out[8]: (40, 8)

yellow 是 160 字节 - 这是最后一行的开始(从末尾开始 40)

yellow is 160 bytes in - that's the start of the last row (40 from the end)

In [9]: 2016-1856
Out[9]: 160
In [10]: my_array_1.nbytes
Out[10]: 200

<小时>

data 地址都不同,但都在同一个范围内.但它们更难解释.


The data addresses all differ, but are in the same ballpark. But they are harder to interpret.

In [11]: my_array_1.data
Out[11]: <memory at 0x7fa975369a68>
In [12]: my_array_red.data
Out[12]: <memory at 0x7fa975369b40>
In [13]: my_array_blue.data
Out[13]: <memory at 0x7fa975369c18>
In [14]: my_array_yellow.data
Out[14]: <memory at 0x7fa9710f11c8>

<小时>

data 属性可以在 ndarray 构造函数中使用:


The data attribute can be used in an ndarray constructor:

yellow 中的两个元素:

In [17]: np.ndarray(2,dtype=my_array_1.dtype,buffer=my_array_yellow.data)
Out[17]: array([20, 21])

相同的 2 个元素,但具有原始地址和偏移量(如上所述):

Same 2 elements, but with the original address, and an offset (as deduced above):

In [18]: np.ndarray(2,dtype=my_array_1.dtype,buffer=my_array_1.data, offset=160)
Out[18]: array([20, 21])

<小时>

实际上,data 显示并没有告诉我们有关数据缓冲区位于何处的任何信息.引用缓冲区的是 memoryview 对象的地址,而不是缓冲区本身的地址.再次调用data,得到一个不同的memoryview对象:


Actually the data display doesn't tell us anything about where the data buffer is located. It's the address of the memoryview object that references the buffer, not the address of the buffer itself. Call data again, and get a different memoryview object:

In [19]: my_array_1.data
Out[19]: <memory at 0x7fa975369cf0>

<小时>

如果我打印这些 memoryview 对象,我会得到与您相同的模式:


If I print these memoryview objects, I get the same pattern as you do:

In [22]: print(my_array_1.data)
<memory at 0x7fa970e37120>
In [23]: print(my_array_red.data)
<memory at 0x7fa970e37120>
In [24]: print(my_array_blue.data)
<memory at 0x7fa970e37120>
In [25]: print(my_array_yellow.data)
<memory at 0x7fa9710f17c8>

对于 23 和 24,它只是重复使用内存插槽,因为打印没有持久性.我不知道为什么 yellow 不重用它,除非对象可能完全不同以至于它不适合同一个空间.在 Out[11] 等情况下,ipython 缓冲挂在这些对象上,因此没有重用.

For 23 and 24, it's just reusing a memory slot, because with print there's no persistence. I'm not sure why yellow doesn't reuse it, except maybe the object is sufficiently different that it doesn't fit in the same space. In the Out[11] etc. cases, the ipython buffering hangs onto those objects, and thus there's not reuse.

它只是强化了这些memoryview 对象 的打印显示没有任何重要意义的想法.它与数据缓冲区位置无关.它更像是 id,只是内存中的任意位置.

It just reinforces the idea that there's nothing significant about the print display of these memoryview objects. It has nothing to do with the databuffer location. It's more like the id, just an arbitrary place in memory.

这篇关于Numpy ,Python3.6 - 无法理解为什么地址不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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