3

I'm very puzzled by the following behaviour of NumPy when assigning elements to an array using an array as indices. Here is a minimal working example:

import numpy as np

i = np.arange(2,4)
a = np.arange(5)
a[:][i] = 0  # this modifies the a array
print(a)

b = np.arange(5)
b[i][:] = 0  # this does NOT modify the b array
print(b)

Why is the a array modified and not the b array? I suspect we are modifying a copy of the b array, but I'm not sure how to show this explicitly. For example, id(a) and id(a[:]) yield different results, yet a is modified.

2
  • The title could be improved. The observed behavior is caused by how slicing and views work, not how assignment works. Commented Jul 22 at 13:31
  • @jabaa Yes, you are right. My apologies for this. I have to admit, I wasn't quite sure how to phrase my question. Commented Jul 22 at 13:38

3 Answers 3

4

a[:] is a view of a, while a and a[:] are different python objects, they share the same underlying memory for the numpy data:

a2 = a[:]

a2.base is a  # True

id(a2), id(a2.base), id(a) # (127331129255728, 127331129258032, 127331129258032)

However, b[i] is a new array, independent from b. b and b[:] each use a different space in memory to store the numpy data:

b2 = b[i]

b2.base # None

There is no good reason why you would need to chain [:][i] or [i][:] in numpy. Just remove the useless [:]:

b[i] = 0

When is a view created?

As explained in the documentation:

Views are created when elements can be addressed with offsets and strides in the original array. Hence, basic indexing always creates views. [...] Advanced indexing, on the other hand, always creates copies.

a[:]       # view (basic indexing)
a[1:3]     # view (basic indexing)
a[1:3][:]  # view (basic indexing)

a[[2, 3]]  # copy (advanced indexing)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for this answer. It clarifies things in my mind. Yes, I agree that it is useless in the present example to add the [:]. In the original program where I encountered this problem, it was a more elaborate version involving multi-dimensional arrays. Here, I was trying to isolate the problem with a somewhat contrived example.
If you're working with multiple dimensions and you're indexing/slicing different dimensions (not like in your example), then a[x, y] should be used if possible, not a[x][y] (not that if you reuse dimensions, that is not necessarily the same!).
Thanks for the advice. Yes, there are some parts in my program which should be modified accordingly. Nonetheless, one needs to be careful with a[x, y] if x and y are arrays. Indeed, numpy extracts [a[xv,yv] for (xv,yv) in zip(x,y)] rather than a[xv,yv] for all combinations of x and y values. To achieve the latter option, I've used things like a[x][:,y], which produces a copy rather than a view (there are probably more elegant solutions than this).
-2

It is because the import command needs to be modified, so that the a and the b array work right. Fix the import command and it should work fine;

3 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
I really doubt this is even an honest attempt to make sense.
Thanks but this doesn't answer my question.
-2

import numpy as np

i = np.arange(2, 4) # i = [2, 3]

a = np.arange(5) # a = [0, 1, 2, 3, 4]

a[i] = 0 # directly modify a at positions 2 and 3

print(a)

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
Thanks but this doesn't answer my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.