Issue
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!
Solution
subset
is not a copy but a view of data
. Thus any change on subset
is a change on data
too.
Answered By - Julien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.