The Python numpy add() function is helpful to perform addition on two arrays. It will compute the addition element-wise, but the two arrays should be in a common shape. They must be broadcastable to a common shape if they are not equal. The syntax of the numpy ndarray add function is
numpy.add(x1, x2, /, out = None, *, where = True, casting = ‘same_kind’, order = ‘K’, type = None, subok = True[, signature, extobj]) = <ufunc ‘add’>
The simplest syntax form is
numpy.add(x1, x2)
If you pass the scalar values, it will return the scalar value. If you pass the array, it returns the ndarray object.
Python numpy array add function Example
In this example, we declared two variables and assigned integer values 10 and 20. It means add() method will perform regular addition.
One-dimensional Example
In this program, we declare two one-dimensional ndarrays of equal size two. Next, we used the numpy array add() method to perform addition.
10 20 = First One
30 40 = Second One
40 60 = Output
So, the calculation = 10 + 30 and 20 + 40
We add a static value of 45 to a in the next line. It means 10 + 45 and 20 + 45.
Two-dimensional Example
In this program, we declared two-dimensional arrays of equal sizes and used the Python numpy add() method on them.
10 20 30 + 15 25 35 = 10+15 20+25 30+35 = 25 45 65
40 50 60 45 35 25 40+45 50+35 60+25 85 85 85