Home > Software engineering >  NumPy behavior - manipulating array changes original data?
NumPy behavior - manipulating array changes original data?

Time:09-28

I have a small array data that undergoes a reshape and a copy

data = np.array([77, 115, 137, 89, 42, 107, 54, 256])

subset = data[:4].reshape(2,2)

newdata = data[4:].copy()

subset[-1,-1] = 0

newdata[1] = 0

print(data)
print(subset)
print(newdata)

Seems pretty simple. My assumption for this is that I would get the following outputs

data = array([ 77, 115, 137, 89, 42, 107, 54, 256])

subset = array([[ 77, 115],
                [137, 0]])

newdata = array([ 42, 0, 54, 256])

I was correct for subset and newdata, but not data itself, which now outputs

data = np.array([77, 115, 137, 0, 42, 107, 54, 256])

The original data array has been modified from what looks like the reshape and copy and changing the 89 to 0

Any help on why and how these methods do in fact modify the original array is greatly appreciated.

Thanks!

CodePudding user response:

subset is not a copy but a view of data. Thus any change on subset is a change on data too.

CodePudding user response:

As @Julien point out, you are dragging the variable anytime you make changes based on data. So to keep your variable untouched, you can use deepcopy:

import copy

data = np.array([77, 115, 137, 89, 42, 107, 54, 256])
data2 = copy.deepcopy(data)
subset = data[:4].reshape(2,2)
newdata = data[4:].copy()
subset[-1,-1] = 0
newdata[1] = 0

print(data) # [ 77 115 137   0  42 107  54 256]
print(data2) # [ 77 115 137  89  42 107  54 256]
  • Related