Let's quote numpy manual: https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing
Advanced indexing is triggered when the selection object, obj, is a non-tuple sequence object, an ndarray (of data type integer or bool), or a tuple with at least one sequence object or ndarray (of data type integer or bool). There are two types of advanced indexing: integer and Boolean.
Advanced indexing always returns a copy of the data (contrast with basic slicing that returns a view).
Then operating on what was returned by advanced indexing should never modify the original array. And indeed:
import numpy as np
arr = np.array([0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
indexes = np.array([3, 6, 4])
slicedArr = arr[indexes]
slicedArr *= 5
arr
This prints:
array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
However, this does not always seem to be the case. Oddly if I don't save whatever was returned by the []
operator to an intermediate variable I am somehow operating on the original array. Please consider this example:
import numpy as np
arr = np.array([0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
indexes = np.array([3, 6, 4])
arr[indexes] *= 5
arr
This prints:
array([ 0, 10, 20, 150, 200, 50, 300, 70, 80, 90])
I do not complain. Actually, this is a life saver for me. Yet, I fail to understand why does this work and I'd really like to understand this.
To my best understanding, as soon as I write arr[indexes]
I am creating a copy of the array; so the subsequent *= 5
should operate on this copy and not on the original array. The result of this calculation should, however, be discarded, since it is not written to any variable.
Yet manifestly I am wrong.
Where is my misunderstanding?