Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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).

like image 892
Gio Avatar asked Aug 13 '15 12:08

Gio


People also ask

How do you find the difference between two arrays in NumPy?

Step 1: Import numpy. Step 2: Define two numpy arrays. Step 3: Find the set difference between these arrays using the setdiff1d() function. Step 4: Print the output.

What is the benefit of broadcasting across NumPy's arrays?

Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. It does this without making needless copies of data and usually leads to efficient algorithm implementations.

What is the difference between NumPy array and normal array?

There are several important differences between NumPy arrays and the standard Python sequences: NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically). Changing the size of an ndarray will create a new array and delete the original.

Are two NumPy arrays equal?

Comparing two NumPy arrays determines whether they are equivalent by checking if every element at each corresponding index is the same. Method 1: We generally use the == operator to compare two NumPy arrays to generate a new array object.


1 Answers

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]]) 
like image 98
poke Avatar answered Sep 21 '22 13:09

poke