Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "numpy.add(a,b)" and "a+b"?

Is there any difference between numpy.add(a,b) and a+b when adding two ndarrays a and b? The documentation says that numpy.add is the "Equivalent to x1 + x2 in terms of array broadcasting.". But I don't unserstand what this means, since numpy.add(numpy.array([1,2,3]),4) also works.

like image 296
Manu Avatar asked Aug 03 '16 15:08

Manu


People also ask

What is difference between NumPy 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.

How does NumPy add work?

NumPy sum adds up the values of a NumPy arrayIt just takes the elements within a NumPy array (an ndarray object) and adds them together. Having said that, it can get a little more complicated. It's possible to also add up the rows or add up the columns of an array.

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

If you want the absolute element-wise difference between both matrices, you can easily subtract them with NumPy and use numpy. absolute on the resulting matrix. Alternatively (although unnecessary), if you were required to do so in native Python you could zip the dimensions together in a nested list comprehension.

Can a NumPy array have different types?

The elements of a NumPy array must all be of the same type, whereas the elements of a Python list can be of completely different types.


1 Answers

In Python syntax, a+b is translated to a.__add__(b). a.__add__ is a method that implements addition for objects of type a. A number has such a method, a list does as well ([1,3]+[4]) and so does a string ('abc'+'d').

numpy has implemented __add__ (and other standard __... methods) for its ndarray class (at least for the numeric dtypes).

That's all standard Python and numpy and been around for ever.

np.add is a ufunc. Look at its doc - see the out parameter, and the Binary ufuncs: section. It's a function, and has some methods like reduce, reduceat, etc that a.__add__ (and +) does not.

If you have two arrays or numbers and you want to sum them, the natural thing is to use +, a+b. np.add is useful in some special cases

Compare, for example, what happens to two lists:

In [16]: [1,2,3]+[4]
Out[16]: [1, 2, 3, 4]     # default list concatenation
In [17]: np.add([1,2,3],[4])
Out[17]: array([5, 6, 7])   # convert lists to arrays and sum

or an example using 2d broadcasting:

In [19]: np.add([[1],[2],[3]],[4,1])
Out[19]: 
array([[5, 2],
       [6, 3],
       [7, 4]])
In [20]: np.array([1,2,3])[:,None]+np.array([4,1])
Out[20]: 
array([[5, 2],
       [6, 3],
       [7, 4]])

And your example:

In [21]: numpy.add(numpy.array([1,2,3]),4)
Out[21]: array([5, 6, 7])
In [22]: numpy.array([1,2,3])+4
Out[22]: array([5, 6, 7])

"Equivalent to x1 + x2 in terms of array broadcasting." means, they both work and do the same thing.

broadcasting is another subject.

==================

The @ operator and np.matmul parallel isn't quite the same. The @ operator is a recent addition to the Python interpreter. It is translated to a call to the __matmul__ method - if defined. New numpy versions have such a definition. But the method is not defined for Python numbers or lists. There is also a function version, np.matmul, referencing the same code. There is also a x.dot and np.dot pairing, but no Python recognized operator or x.__dot__.

like image 163
hpaulj Avatar answered Sep 20 '22 19:09

hpaulj