Skip to content
Advertisement

unexpected result in numpy array slicing (view vs copy)

I’m trying to reduce the amount of copying in my code and I came across surprising behavior when dealing with numpy array slicing and views, as explained in:

Scipy wiki page on copying numpy arrays

I’ve stumbled across the following behavior, which is unexpected for me:

Case 1.:

JavaScript

As expected, this outputs:

JavaScript

Case 2: When performing the slicing and addition in one line, things look different:

JavaScript

The part that’s surprising to me is that a[:,1:2] does not seem to create a view, which is then used as a left hand side argument, so, this outputs:

JavaScript

Maybe someone can shed some light on why these two cases are different, I think I’m missing something.

Solution: I missed the obvious fact that the “+” operator, other than the in-place operator “+=” will always create a copy, so it’s in fact not related but slicing other than how in-place operators are defined for numpy arrays.

To illustrate this, the following generates the same output as Case 2:

JavaScript

Advertisement

Answer

The above is no different than:

JavaScript

Which, at least to me, seem like completely expected behavior. The b+=x operator calls __iadd__ which importantly first tries to modify the array in place, so it will update b which is still a view of a. While the b=b+x operator calls __add__ which creates new temporary data and then assigns it to b.

For a[i] +=b the sequence is (in numpy):

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement