Python Numpy矩阵行/列内存布局

Python Numpy矩阵行/列内存布局,python,arrays,numpy,memory,Python,Arrays,Numpy,Memory,Numpy nd阵列被布置为连续的1-d阵列 此堆栈溢出对话()表明,矩阵的行索引是原始数组中的“视图”,并且在索引时没有在内存中分配新的数组对象 然而,当我查看numpy矩阵中各行的地址位置时,会得到奇怪的结果。比如说, >> x = numpy.random.rand(2,2) >> x.data <read-write buffer for 0x7fdc92a941c0, size 32, offset 0 at 0x7fdc92ad2d30> &

Numpy nd阵列被布置为连续的1-d阵列

此堆栈溢出对话()表明,矩阵的行索引是原始数组中的“视图”,并且在索引时没有在内存中分配新的数组对象

然而,当我查看numpy矩阵中各行的地址位置时,会得到奇怪的结果。比如说,

>> x = numpy.random.rand(2,2)
>> x.data
   <read-write buffer for 0x7fdc92a941c0, size 32, offset 0 at 0x7fdc92ad2d30>
>> x[0,].data
   <read-write buffer for 0x7fdc92a94a30, size 16, offset 0 at 0x7fdc929e7270>
>> x[1,].data
   <read-write buffer for 0x7fdc92a94080, size 16, offset 0 at 0x7fdc929e7030>

如果在索引矩阵
x
的行时没有创建新的数组对象,为什么这些数组视图在内存中相距如此之远?内存中数组视图之间的距离是否会破坏跨行/跨列操作的矩阵操作的缓存性能?

。\uuuuuu数组\u接口\uuuu
提供更好的显示

In [498]: x=np.random.rand(2,2)
In [499]: x.__array_interface__['data']
Out[499]: (182019952, False)
In [500]: x[0].__array_interface__['data']
Out[500]: (182019952, False)
In [501]: x[0,:].__array_interface__['data']
Out[501]: (182019952, False)
In [502]: x[1,:].__array_interface__['data']
Out[502]: (182019968, False)
这些地址都相同或相近

选择单个项目将产生不同的地址

In [504]: x[0,0].__array_interface__['data']
Out[504]: (182020568, False)

这不是一个视图,它是一个
np。float64
。\uuuu数组\u接口\uuuu
.data
显示得更好

In [498]: x=np.random.rand(2,2)
In [499]: x.__array_interface__['data']
Out[499]: (182019952, False)
In [500]: x[0].__array_interface__['data']
Out[500]: (182019952, False)
In [501]: x[0,:].__array_interface__['data']
Out[501]: (182019952, False)
In [502]: x[1,:].__array_interface__['data']
Out[502]: (182019968, False)
这些地址都相同或相近

选择单个项目将产生不同的地址

In [504]: x[0,0].__array_interface__['data']
Out[504]: (182020568, False)

这不是一个视图,而是一个
np.float64

位0x7fdc92ad2d30的
不指示缓冲区内容的存储位置。它指示缓冲区对象头所在的位置——一个包含一堆元数据和指向缓冲区内容的指针的小东西。缓冲区标头的位置完全独立于其内容的位置

例如,由于
x.data
是动态生成的,因此0xwhere
位的
在每次访问时都可以表示完全不同的内容:

In [7]: x = numpy.arange(8)

In [8]: x.data
Out[8]: <read-write buffer for 0x24a6b60, size 64, offset 0 at 0x7f3a7b3438f0>

In [9]: x.data
Out[9]: <read-write buffer for 0x24a6b60, size 64, offset 0 at 0x7f3a7b343970>
[7]中的
:x=numpy.arange(8)
在[8]:x.data中
出[8]:
在[9]:x.data中
出[9]:

0x7fdc92ad2d30处的
位不指示缓冲区内容的存储位置。它指示缓冲区对象头所在的位置——一个包含一堆元数据和指向缓冲区内容的指针的小东西。缓冲区标头的位置完全独立于其内容的位置

例如,由于
x.data
是动态生成的,因此0xwhere
位的
在每次访问时都可以表示完全不同的内容:

In [7]: x = numpy.arange(8)

In [8]: x.data
Out[8]: <read-write buffer for 0x24a6b60, size 64, offset 0 at 0x7f3a7b3438f0>

In [9]: x.data
Out[9]: <read-write buffer for 0x24a6b60, size 64, offset 0 at 0x7f3a7b343970>
[7]中的
:x=numpy.arange(8)
在[8]:x.data中
出[8]:
在[9]:x.data中
出[9]: