1

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!

1
  • 3
    You can read copies and views in the official documentation. Commented Sep 28, 2022 at 5:01

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Basic indexing as with [:4] produces a view. reshape usually produces a view as well. But sometimes the combination produces a copy. In this case is was still a view, but we need to be careful. There have a lot of SO about testing whether something is indeed a view or not.
1

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]

3 Comments

You don't need deepcopy with arrays - unless it is object dtype.
Hi @hpaulj, I just point out a way to solve the issue of the OP. You are welcome to post your own solution as well. Regards.
data2 = data.copy() is enough.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.