Weird behaviour with numpy array operations
arrays, numpy, python
Solution
When you slice a numpy array as you are doing in the example, you get a view of the data rather than a copy.
See:
http://scipy-lectures.github.io/advanced/advanced_numpy/#example-inplace-operations-caveat-emptor
Problem
Explain this: ``` >>> a = np.arange(10) >>> a[2:] array([2, 3, 4, 5, 6, 7, 8, 9]) >>> a[:-2] array([0, 1, 2, 3, 4, 5, 6, 7]) >>> a[2:] - a[:-2] array([2, 2, 2, 2, 2, 2, 2, 2]) >>> a[2:] -= a[:-2] >>> a array([0, 1, 2, 2, 2, 3, 4, 4, 4, 5]) ``` The expected result is of course `array([0, 1, 2, 2, 2, 2, 2, 2, 2, 2])`. I'm going to guess this is something to do with numpy parallelising things and not being smart enough to work out that it needs to make a temporary copy of the data first (or do the operation in the correct order). In other words I suspect it is doing something naive like this: ``` for i in range(2, len-2): a[i] -= a[i-2] ``` For reference it works in Matlab and Octave: ``` a = 0:9 a(3:end) = a(3:end) - a(1:end-2) a = 0 1 2 3 4 5 6 7 8 9 a = 0 1 2 2 2 2 2 2 2 2 ``` And actually it works fine if you do: ``` a[2:] = a[2:] - a[:-2] ``` So presumably this means that `a -= b` is not the same as `a = a - b` for numpy! Actually now that I come to think of it, I think Mathworks gave this as one of the reasons for not implementing the +=, -=, /= and *= operators!