Numpy数组共享内存,即使所有测试都为假?这是一个错误吗?

问题描述

我有一些代码,而且我无法一生都无法弄清楚它们是如何共享内存的!!! 所有测试都是错误的(如图所示),但是内存位置似乎在功能,外观上保持一致我花了几个小时尝试进行故障排除,无法解决请帮助!!!为什么这些数组共享内存!?

listofValues listofxValues 不应共享内存。

警告:丑陋的代码,我很抱歉,这是艰难的一天。

listofLists = []
splitold=0

#split arrays into each bin
for split in splitInd:
    splitnew=split
    listofLists.append(xind[splitold:splitnew])
    splitold=split

listofValues = np.array(listofLists.copy())
#convert to float type
for i,lst in enumerate(listofValues):
    listofValues[i] = lst.astype(float)

listofxValues = np.copy(listofValues)

#PRINT TROUBLESHOOTING
print(listofxValues.data)
print(listofValues.data)
print(listofValues is listofxValues)
print(listofValues == listofxValues)
print(listofValues.data is listofxValues.data)
print(listofValues.data == listofxValues.data)
print(np.shares_memory(listofxValues,listofValues))
print(listofxValues.__array_interface__['data'][0] == listofValues.__array_interface__['data'][0]) 
print(listofxValues.__array_interface__['data'][0] == listofValues.__array_interface__['data'][0])

#exchange indexed values for real values
for each in range(len(listofValues)):
    for i,index in enumerate(listofLists[each]):
        listofxValues[each][i] = xdata[index]
        listofValues[each][i] = ydata[index]

maxArrayInd = []
#get max indices
for array in listofValues:
    maxArrayInd.append(np.argmax(array))

maxX = []
maxY = []
#get values for max indices
for idx,mx in enumerate(maxArrayInd):
    maxX.append(listofxValues[idx][mx])
    maxY.append(listofValues[idx][mx])

代码输出

<memory at 0x00000215644C5640>
<memory at 0x00000215644C5640>
False
False
False
False
False
False
False

尽管所有测试结果都是错误的,但是内存位置显然是相同的。它破坏了我的代码。 请帮忙!

谢谢你,请绝望。

编辑:

我无法弄清楚为什么它会这样,而是移动

listofxValues = np.copy(listofValues)

上面的#convert为浮点类型修复了该错误。我仍然不知道为什么他们共享内存,以及为什么要解决这个问题。我了解了指针,视图,副本等之间的区别(至少是基本的理解),但这仍然使我为什么遇到问题没有任何意义。

感谢您的所有回答!我至少学到了一些!

解决方法

.data属性是一个memoryview对象。 每次访问该属性都会创建一个新的memoryview打印memoryview时显示的地址是Python对象的地址,而不是该对象中基础数据的地址。数组。

当Python执行print(listOfxValues.data)时,访问.data属性将触发NumPy代码,该代码创建一个新的memoryview,并将该对象传递给print()。对print()的调用完成后,不再有任何保存对memoryview的引用的Python对象,因此可以由垃圾回收器释放它。然后,当您调用print(listOfValues.data)时,会创建一个新的memoryview对象,但是事实证明Python恰好重用了上一次调用中使用的相同内存。

如果您这样做:

a = listOfxValues.data
b = listOfValues.data
print(a)
print(b)

两个memoryview的内存地址将始终不同。 如果ab是通过访问相同数组.data属性(例如

)创建的,则也是如此
In [23]: x = np.array([1,2,3,4])

In [24]: a = x.data

In [25]: b = x.data

In [26]: a
Out[26]: <memory at 0x120975050>

In [27]: b
Out[27]: <memory at 0x120808c80>
,

对象dtype是您观察到的行为的最佳解释。我会说明。

如果我创建一个对象dtype数组(1.19版现在需要显式对象dtype):

In [55]: x = np.array([[1,2],[3,4,5]],object)                                                       
In [56]: x                                                                                           
Out[56]: array([list([1,2]),list([3,5])],dtype=object)

和副本:

In [57]: xc = x.copy()                                                                               

“数据”缓冲区的位置不同:

In [58]: x.__array_interface__['data'][0]                                                            
Out[58]: 35624384
In [59]: xc.__array_interface__['data'][0]                                                           
Out[59]: 40985936

但是副本的第一个元素与原始元素相同:

In [60]: id(x[0])                                                                                    
Out[60]: 139816762070344
In [61]: id(xc[0])                                                                                   
Out[61]: 139816762070344

也就是说,它是相同的list对象。修改一个会修改另一个:

In [62]: x[0][1] = 10         # can't use x[0,1] = 10 syntax                                                                         
In [63]: x                                                                                           
Out[63]: array([list([1,10]),dtype=object)
In [64]: xc                                                                                          
Out[64]: array([list([1,dtype=object)
x.copy()是数字dtype而不是对象dtype时,

x很好。与列表一样,如果您希望它们独立,则需要deepcopy。即使很容易创建对象,也不要天真地使用对象dtype数组!