Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Numpy treats a+=b and a=a+b differently

Tags:

Is the following numpy behavior intentional or is it a bug?

from numpy import *  a = arange(5) a = a+2.3 print 'a = ', a # Output: a = 2.3, 3.3, 4.3, 5.3, 6.3   a = arange(5) a += 2.3 print 'a = ', a # Output: a = 2, 3, 4, 5, 6 

Python version: 2.7.2, Numpy version: 1.6.1

like image 486
Dhara Avatar asked May 24 '12 14:05

Dhara


People also ask

What is a NumPy array how they are different from lists?

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. The elements in a NumPy array are all required to be of the same data type, and thus will be the same size in memory.

What is the difference between array module and NumPy array?

And inbuilt array module when the desired data type of array was Unicode character specified by typecode 'u', and the floating value was sent to array. TypeError occurred that 'array item must be Unicode character'. But in numpy array when the desired data type of array was int and float value was sent to array.

Why we use import NumPy as NP in Python?

The import numpy portion of the code tells Python to bring the NumPy library into your current environment. The as np portion of the code then tells Python to give NumPy the alias of np. This allows you to use NumPy functions by simply typing np. function_name rather than numpy.

What is the function of NumPy in Python?

NumPy is a Python library used for working with arrays. It also has functions for working in domain of linear algebra, fourier transform, and matrices. NumPy was created in 2005 by Travis Oliphant.


2 Answers

That's intentional.

The += operator preserves the type of the array. In other words, an array of integers remains an array of integers.

This enables NumPy to perform the += operation using existing array storage. On the other hand, a=a+b creates a brand new array for the sum, and rebinds a to point to this new array; this increases the amount of storage used for the operation.

To quote 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.

Finally, if you're wondering why a was an integer array in the first place, consider the following:

In [3]: np.arange(5).dtype Out[3]: dtype('int64')  In [4]: np.arange(5.0).dtype Out[4]: dtype('float64') 
like image 113
NPE Avatar answered Oct 30 '22 05:10

NPE


@aix is completely right. I just wanted to point out this is not unique to numpy. For example:

>>> a = [] >>> b = a >>> a += [1] >>> print a [1] >>> print b [1] >>> a = a + [2] >>> print a [1, 2] >>> print b [1] 

As you can see += modifies the list and + creates a new list. This hold for numpy as well. + creates a new array so it can be any data type. += modifies the array inplace and it's not practical, and imo desirable, for numpy to change the data type of an array when array content is modified.

like image 37
Bi Rico Avatar answered Oct 30 '22 07:10

Bi Rico