Gio
Gio

Reputation: 3340

numpy array, difference between a /= x vs. a = a / x

I'm using python 2.7.3, when I execute the following piece of code:

import numpy as np

a = np.array([[1,2,3],[4,5,6]])
a = a / float(2**16 - 1)
print a

This will result in he following output:

>> array([[1.52590219e-05, 3.05180438e-05, 4.57770657e-05],
>>       [6.10360876e-05, 7.62951095e-05, 9.15541314e-05]])

Exactly as expected, however when I execute the following piece of code:

import numpy as np

a = np.array([[1,2,3],[4,5,6]])
a /= float(2**16 - 1)
print a

I get the following output:

>> array([[0, 0, 0],
>>        [0, 0, 0]])

I expected the same output as in the previous example, I don't understand the different ouput, which seems to be a result of using a /= float(2**16 - 1) vs a = a / float(2**16 - 1).

Upvotes: 23

Views: 3532

Answers (1)

poke
poke

Reputation: 388163

From the documentation:

Warning:

In place operations will perform the calculation using the precision decided by the data type of the two operands, but will silently downcast the result (if necessary) so it can fit back into the array. Therefore, for mixed precision calculations, A {op}= B can be different than A = A {op} B. For example, suppose a = ones((3,3)). Then, a += 3j is different than a = a + 3j: while they both perform the same computation, a += 3 casts the result to fit back in a, whereas a = a + 3j re-binds the name a to the result.

Since your array was an array of integers, when using the in-place operations, the result will be downcasted to integers again.

If you change your array so it stores floats originally, then the results (which are floats) can be stored in the original array, and your code will work fine:

>>> a = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
>>> a /= float(2**16 - 1)
>>> a
array([[  1.52590219e-05,   3.05180438e-05,   4.57770657e-05],
       [  6.10360876e-05,   7.62951095e-05,   9.15541314e-05]])

Upvotes: 36

Related Questions