131

For example, if we have a numpy array A, and we want a numpy array B with the same elements.

What is the difference between the following (see below) methods? When is additional memory allocated, and when is it not?

  1. B = A
  2. B[:] = A (same as B[:]=A[:]?)
  3. numpy.copy(B, A)
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45
mrgloom
  • 20,061
  • 36
  • 171
  • 301

3 Answers3

163

All three versions do different things:

  1. B = A

    This binds a new name B to the existing object already named A. Afterwards they refer to the same object, so if you modify one in place, you'll see the change through the other one too.

  2. B[:] = A (same as B[:]=A[:]?)

    This copies the values from A into an existing array B. The two arrays must have the same shape for this to work. B[:] = A[:] does the same thing (but B = A[:] would do something more like 1).

  3. numpy.copy(B, A)

    This is not legal syntax. You probably meant B = numpy.copy(A). This is almost the same as 2, but it creates a new array, rather than reusing the B array. If there were no other references to the previous B value, the end result would be the same as 2, but it will use more memory temporarily during the copy.

    Or maybe you meant numpy.copyto(B, A), which is legal, and is equivalent to 2?

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • 27
    @Mr_and_Mrs_D: Numpy arrays work differently than lists do. Slicing an array does not make a copy, it just creates a new view on the existing array's data. – Blckknght Sep 29 '17 at 10:35
  • 1
    What is meant by `but B = A[:] would do something more like 1` ? According to this https://stackoverflow.com/a/2612815 `new_list = old_list[:]` is also a copy. – mrgloom May 23 '18 at 12:13
  • 5
    @mrgloom: Numpy arrays work differently than lists when it comes to slicing and copying their contents. An array is a "view" of an underlying block of memory where the numeric values are stored. Doing a slice like `some_array[:]` will create a new array object, but that new object will be a view of the same memory as the original array, which won't have been copied. That's why I said it's more like `B = A`. It takes only `O(1)` space and time, rather than the `O(n)` of each a real copy would need. – Blckknght May 23 '18 at 23:19
  • The preferred 'copy' method, according to the documentation, is `B = A.copy()`. However this form doesn't preserve order by default, you need `B = A.copy(order='k')`. – Howard Lovatt Oct 15 '20 at 06:37
35
  1. B=A creates a reference
  2. B[:]=A makes a copy
  3. numpy.copy(B,A) makes a copy

the last two need additional memory.

To make a deep copy you need to use B = copy.deepcopy(A)

Mailerdaimon
  • 6,003
  • 3
  • 35
  • 46
  • 2
    Refering to your second example: `B[:] = A` does **not** make a deep copy of arrays of object-type, e.g. `A = np.array([[1,2,3],[4,5]]); B = np.array([None,None], dtype='O')`. Now try `B[:] = A; B[0][0]=99`, this will change the first element in both **A and B**! To my knowledge, there is no other way to guarantee a deep copy, even of a numpy-array, than `copy.deepcopy` – Rolf Bartstra May 01 '18 at 12:38
17

This is the only working answer for me:

B=numpy.array(A)
Sadegh
  • 865
  • 1
  • 23
  • 47