1

I noticed some unexpected behaviour when working with a copy of an array. For instance, I have a NumPy array (a):

import numpy as np
a = np.random.randint(9, size=(4,4))

Output:

array([[3, 4, 4, 3],
       [0, 0, 4, 2],
       [6, 3, 1, 6],
       [1, 5, 5, 5]])

Then, I make a copy of this array (b) to manipulate the copy and keep the original intact:

b = a #copy of the array
b[b == 2] = 0 #manipulating the copy

However, it appears that both the original and the copy are now changed:

b = array([[3, 4, 4, 3],
           [0, 0, 4, 0],
           [6, 3, 1, 6],
           [1, 5, 5, 5]])

a = array([[3, 4, 4, 3],
           [0, 0, 4, 0],
           [6, 3, 1, 6],
           [1, 5, 5, 5]])

I don't understand why the original array is changed when the manipulation was only applied to the copy. This is quite different to what I would expect from Matlab or R. Is there a way of preventing this behaviour?

Joe Bathelt
  • 5,459
  • 2
  • 15
  • 14

1 Answers1

2

With b = a you are only copying the reference of the object not the object itself. Have a look at this copy function. In short you have to do, b = a.copy()

Hossain Muctadir
  • 3,546
  • 1
  • 19
  • 33