Python np.array 的 np.array 的深拷贝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37593013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deep copy of a np.array of np.array
提问by Dominik Müller
I have a numpy array of different numpy arrays and I want to make a deep copy of the arrays. I found out the following:
我有一个由不同 numpy 数组组成的 numpy 数组,我想制作这些数组的深层副本。我发现了以下内容:
import numpy as np
pairs = [(2, 3), (3, 4), (4, 5)]
array_of_arrays = np.array([np.arange(a*b).reshape(a,b) for (a, b) in pairs])
a = array_of_arrays[:] # Does not work
b = array_of_arrays[:][:] # Does not work
c = np.array(array_of_arrays, copy=True) # Does not work
d = np.array([np.array(x, copy=True) for x in array_of_arrays])
array_of_arrays[0][0,0] = 100
print a[0][0,0], b[0][0,0], c[0][0,0], d[0][0,0]
Is d the best way to do this? Is there a deep copy function I missed? And what is the best way to interact with each element in this array of different sized arrays?
d 是最好的方法吗?是否有我错过的深度复制功能?与这个由不同大小数组组成的数组中的每个元素进行交互的最佳方式是什么?
回答by Tomasz Plaskota
import numpy as np
import copy
pairs = [(2, 3), (3, 4), (4, 5)]
array_of_arrays = np.array([np.arange(a*b).reshape(a,b) for (a, b) in pairs])
a = copy.deepcopy(array_of_arrays)
Feel free to read up more about this here.
Oh, here is simplest test case:
哦,这是最简单的测试用例:
a[0][0,0]
print a[0][0,0], array_of_arrays[0][0,0]
回答by maschu
Beaten by one minute. Indeed, deepcopy is the answer here.
被打了一分钟。事实上,deepcopy 就是答案。
To your second question abut indexing: I have a feeling that you may be better off with a simple list or a dictionary-type data structure here. np.arrays make sense primarily if each array element is of the same type. Of course you can argue that each element in array_of_arrays is another array, but what is the benefit of having them collected in a numpy array instead of a simple list?
关于与索引有关的第二个问题:我有一种感觉,在这里使用简单的列表或字典类型的数据结构可能会更好。np.arrays 主要是在每个数组元素是相同类型时才有意义。当然,你可以争辩说 array_of_arrays 中的每个元素都是另一个数组,但是将它们收集在一个 numpy 数组而不是一个简单的列表中有什么好处?
list_of_arrays = [np.arange(a*b).reshape(a,b) for (a, b) in pairs]
回答by hpaulj
In [276]: array_of_arrays
Out[276]:
array([array([[0, 1, 2],
[3, 4, 5]]),
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]),
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])], dtype=object)
array_of_arrays
is dtype=object
; that means each element of the array is a pointer to an object else where in memory. In this case those elements are arrays of different sizes.
array_of_arrays
是dtype=object
;这意味着数组的每个元素都是指向内存中其他对象的指针。在这种情况下,这些元素是不同大小的数组。
a = array_of_arrays[:]
a
is a new array, but a view of array_of_arrays
; that is, it has the same data buffer (which in this case is list of pointers).
a
是一个新数组,但是一个视图array_of_arrays
;也就是说,它具有相同的数据缓冲区(在这种情况下是指针列表)。
b = array_of_arrays[:][:]
this is just a view of a view. The second [:]
acts on the result of the first.
这只是一种看法。第二个[:]
作用于第一个的结果。
c = np.array(array_of_arrays, copy=True)
This is the same as array_of_arrays.copy()
. c
has a new data buffer, a copy of the originals
这与array_of_arrays.copy()
. c
有一个新的数据缓冲区,原件的副本
If I replace an element of c
, it will not affect array_of_arrays
:
如果我替换 的元素c
,它不会影响array_of_arrays
:
c[0] = np.arange(3)
But if I modify an element of c
, it will modify the same element in array_of_arrays
- because they both point to the same array.
但是如果我修改 的元素c
,它将修改相同的元素 in array_of_arrays
- 因为它们都指向同一个数组。
The same sort of thing applies to nested lists of lists. What array
adds is the view
case.
同样的事情适用于列表的嵌套列表。什么array
增加是view
如此。
d = np.array([np.array(x, copy=True) for x in array_of_arrays])
In this case you are making copies of the individual elements. As others noted there is a deepcopy
function. It was designed for things like lists of lists, but works on arrays as well. It is basically doing what you do with d
; recursively working down the nesting tree.
在这种情况下,您正在制作各个元素的副本。正如其他人指出的那样,有一个deepcopy
功能。它是为列表之类的东西设计的,但也适用于数组。它基本上是在做你做的事情d
;递归地沿着嵌套树向下工作。
In general, an object array is like list nesting. A few operations cross the object boundary, e.g.
一般来说,对象数组就像列表嵌套。一些操作跨越对象边界,例如
array_of_arrays+1
but even this effectively is
但即使这样有效的是
np.array([x+1 for x in array_of_arrays])
One thing that a object array adds, compared to a list, is operations like reshape
. array_of_arrays.reshape(3,1)
makes it 2d; if it had 4 elements you could do array_of_arrays.reshape(2,2)
. Some times that's handy; other times it's a pain (it's harder to iterate).
与列表相比,对象数组添加的一件事是像reshape
. array_of_arrays.reshape(3,1)
使其成为 2d;如果它有 4 个元素,你可以做array_of_arrays.reshape(2,2)
。有时这很方便;其他时候很痛苦(迭代更难)。