Deep copy of a np.array of np.array

前端 未结 3 1475
我寻月下人不归
我寻月下人不归 2020-12-20 11:22

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:

import numpy as np

pairs = [(2, 3),          


        
3条回答
  •  生来不讨喜
    2020-12-20 12:12

    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.

    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).

    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

    If I replace an element of c, it will not affect 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.

    The same sort of thing applies to nested lists of lists. What array adds is the view case.

    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.

    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).

提交回复
热议问题