简体   繁体   English

Numpy深拷贝仍在改变原始阵列

[英]Numpy deep copy still altering original array

As I understand it, a deep copy of an ndarray should create a second iteration of the ndarray so that changing either array will not affect the contents of the other. 据我所知,ndarray的深层副本应该创建ndarray的第二次迭代,这样更改任一数组都不会影响另一个数组的内容。 However, in the following code, my original ndarray is changed: 但是,在以下代码中,我的原始ndarray已更改:

print(data[3])   #[array([[0.00000000e+00, 3.29530000e+04],
   #[4.00066376e-04, 3.29530000e+04],
   #[8.00132751e-04, 3.29530000e+04],
   #...,
   #[1.28784461e+03, 3.47140000e+04],
   #[1.28784621e+03, 3.57750000e+04],
   #[1.28785381e+03, 1.92450000e+04]]),
   #'CH4.VDC1']

new = np.empty_like(data)
new[:] = data
new[3][0][:,1] = 4/16421 * (data[3][0][:,1] - 33563)

print(data[3])  #[array([[ 0.00000000e+00, -1.48590220e-01],
   #[ 4.00066376e-04, -1.48590220e-01],
   #[ 8.00132751e-04, -1.48590220e-01],
   #...,
   #[ 1.28784461e+03,  2.80372694e-01],
   #[ 1.28784621e+03,  5.38822240e-01],
   #[ 1.28785381e+03, -3.48772913e+00]]),
   #'CH4.VDC1']

The array is a mixed type (5,2) array with a (largenumber,2) subarray inside. 该数组是一个混合类型(5,2)数组,内部有一个(largenumber,2)子数组。 I am only trying to change the subarray but I am wondering if the deep copy extends to that subarray as well. 我只是想改变子阵列,但我想知道深拷贝是否也扩展到了这个子阵列。 I have run 我跑了

np.shares_memory(new, data) #false

np.might_share_memory(new,data) #false

It might also be important to note that I am running this in a jupyter notebook. 值得注意的是,我在一个jupyter笔记本中运行它。 Although I can't imagine why it would change anything. 虽然我无法想象它为什么会改变任何东西。 You can recreate data with: 您可以使用以下方式重新创建数

np.array([[[[0.00000000e+00, 2.82540000e+04],
[4.00066376e-04, 2.82530000e+04],
[8.00132751e-04, 2.82520000e+04],
[1.28784461e+03, 4.61170000e+04],
[1.28784621e+03, 3.38280000e+04],
[1.28785381e+03, 3.38230000e+04]],
'CH1.Bx'],
[[[0.00000000e+00, 2.00400000e+04],
[4.00066376e-04, 2.00400000e+04],
[8.00132751e-04, 2.00410000e+04],
[1.28784461e+03, 1.81600000e+04],
[1.28784621e+03, 1.80830000e+04],
[1.28785381e+03, 4.80200000e+03]],
'CH2.By'],
[array([[0.00000000e+00, 3.82520000e+04],
[4.00066376e-04, 3.82510000e+04],
[8.00132751e-04, 3.82510000e+04],
[1.28784461e+03, 3.42810000e+04],
[1.28784621e+03, 3.42820000e+04],
[1.28785381e+03, 3.40380000e+04]]),
'CH3.Bz'],
[[[ 0.00000000e+00, -1.48590220e-01],
[ 4.00066376e-04, -1.48590220e-01],
[ 8.00132751e-04, -1.48590220e-01],
[ 1.28784461e+03,  2.80372694e-01],
[ 1.28784621e+03,  5.38822240e-01],
[ 1.28785381e+03, -3.48772913e+00]],
'CH4.VDC1'],
[[[0.00000000e+00, 3.26760000e+04],
[4.00066376e-04, 3.26760000e+04],
[8.00132751e-04, 3.26750000e+04],
[1.28784981e+03, 3.40450000e+04],
[1.28785061e+03, 3.40420000e+04],
[1.28785141e+03, 3.40390000e+04]],
'CH5.VDC2']], dtype=object)`

You should use copy's deepcopy , [:] does not do a deepcopy: 你应该使用copy的deepcopy ,[:]不进行深度检查:

In [11]: a = [[1], 2]

In [12]: b = a[:]

In [13]: from copy import deepcopy
    ...: c = deepcopy(a)

In [14]: b[0].append(3)

In [15]: a
Out[15]: [[1, 3], 2]

In [16]: c
Out[16]: [[1], 2]

That doesn't look like an array you're starting with there. 这看起来不像你从那里开始的数组。 It's not clear what data is, but data[3] is a 2-element list containing an array and a string, and judging by that, data is probably another list, or possibly an object-dtype array. 目前尚不清楚data是什么,但data[3]是一个包含数组和字符串的2元素列表,并且由此判断, data可能是另一个列表,或者可能是object-dtype数组。

Your attempt at a deep copy: 您尝试深层复制:

new = np.empty_like(data)
new[:] = data

is not a deep copy. 不是一个深刻的副本。 It would be a copy for most normal arrays (deep/shallow are equivalent for most arrays), but not a deep copy for a list, and not a deep copy for an object-dtype array. 它将是大多数普通数组的副本(深/浅对于大多数数组是等效的),但不是列表的深拷贝,而不是对象-dtype数组的深拷贝。 It will create a new object-dtype array and fill it with references to the same objects referenced by the cells of data . 它将创建一个新的object-dtype数组,并使用对data单元格引用的相同对象的引用来填充它。

You should probably pick a better way to organize your data. 您应该选择一种更好的方式来组织数据。 This data structure is not an effective way to work with NumPy, and it will cause more problems than just this. 这种数据结构不是与NumPy一起使用的有效方法,它将导致更多问题而不仅仅是这个问题。 That said, if you want to deep copy it, copy.deepcopy is probably your best bet. 也就是说,如果你想深度复制它, copy.deepcopy可能是你最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM