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)
.
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.
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.
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.
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.
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 thanA = A {op} B
. For example, supposea = ones((3,3))
. Then,a += 3j
is different thana = a + 3j
: while they both perform the same computation,a += 3
casts the result to fit back ina
, whereasa = a + 3j
re-binds the namea
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]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With