Numpy Add | How to Use Numpy.add() Function in Python

The Numpy add function is a part of numpy arithmetic operations. There are basic arithmetic operators available in the numpy module, which are add, subtract, multiply, and divide. The significance of python add is equivalent to the addition operation in mathematics.

What does Numpy Add Function do?

The numpy add function calculates the addition between the two arrays. It calculates the addition between the two arrays, say a1 and a2, element-wise. The numpy.add() is a universal function, i.e., supports several parameters that allow you to optimize its work depending on the specifics of the algorithm.

Syntax of Numpy Add

numpy.add(a1, a2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘add’)

Parameters of Numpy Add

ParameterMandatory or Not
a1Mandatory
a2Mandatory
/Not-Mandatory
outNot-Mandatory
*Not-Mandatory
whereNot-Mandatory
castingNot-Mandatory
orderNot-Mandatory
dtypeNot-Mandatory
subokNot-Mandatory
unfuncNot-Mandatory
  • a1: [arrayLike]
    1st Input array for calculating the addition.
  • a2: [arrayLike]
    2nd input array for calculating the addition.
  • out: [ndarray, None, or tuple of ndarray and None, optional]
    out will be the location where the result is to be stored. have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
  • where: [array_like, optional]
    If the value of where is true, it indicates to calculate the unfunc at that position, whereas if the value is false, then it denotes to leave the value in output only.

Return Value of Numpy Add

The add function returns the addition between a1 and a2. The add() function can be scalar of nd-array. It depends on the a1 and a2. If a1 and a2 are scalar, than numpy.add() will return a scalar value. Else it will return an nd-array.

Note: The input a1 and a2 must be broadcastable to a common shape (which becomes the shape of the output).

Examples of Numpy Add Function

Let’s go through the examples of Numpy add() function and see how it works.

Example 1: Using Np.add() Function To add two numbers

import numpy as np
a1 = 24
a2 = 13

print ("1st Input number : ", a1) 
print ("2nd Input number : ", a2) 
	
ad = np.add(a1, a2) 
print ("Addition of two input number : ", ad) 

Output:

1st Input number :  24
2nd Input number :  13
Addition of two input number :  37

Explanation

In this simple first example, we just added two numbers and get the result. Let’s take a look at each step and know what happens in each stage. First of all, we imported the numpy module as np it’s obvious because we are working on the numpy library. After that, we have taken two pre-defined inputs ’24’, ’13’, and stored them in variables ‘a1’, ‘a2’ respectively. We printed our inputs to check whether they are specified properly or not. Then the main part comes where we will find the addition between the two numbers.

Herewith the help of the np.add() function, we will calculate the addition between a1 and a2. This addition operation is identical to what we do in mathematics.

So, we will get the addition between the number 24 and 13 which is 11.

Example 2: Using Np.add() Function to find the Addition between two input arrays

import numpy as np
a1 = [20, 21, 5, 9]
a2 = [13, 17, 6, 11]

print ("1st Input array : ", a1) 
print ("2nd Input array : ", a2) 
	
ad = np.add(a1, a2) 
print ("Addition of two input arrays : ", ad) 

Output:

1st Input array :  [20, 21, 5, 9]
2nd Input array :  [13, 17, 6, 11]
Addition of two input arrays :  [33 38 11 20]

Explanation

From this example, things get Lil bit tricky; instead of numbers, we have used arrays as our input value.
We can now see we have two input arrays a1 & a2 with array inputs [20, 21, 5, 9] and [13, 17, 6, 11], respectively. The add() function will find the addition between a1 & a2 array arguments, element-wise.

So, the solution will be an array with the shape equal to input arrays a1 and a2. The addition between a1 and a2 will be calculated parallelly, and the result will be stored in the ad variable.

Example 3: Using Np.add() Function To Find Addition Between Two Multi-Dimensional Arrays

import numpy as np
a1 = [[20, 21, 5], [-9, 11, 1]]
a2 = [[13, 17, 6], [1, -8, 7]]

print ("1st Input array : ", a1) 
print ("2nd Input array : ", a2) 
	
ad = np.add(a1, a2) 
print ("Addition of two input arrays : ", ad) 

Output:

1st Input array :  [[20, 21, 5], [-9, 11, 1]]
2nd Input array :  [[13, 17, 6], [1, -8, 7]]
Addition of two input arrays :  [[33 38 11]
[-8  3  8]]

Explanation

The third example in this add() function tutorial is slightly similar to the second example which we have already gone through. What we have done here in this example is instead of a simple array we have used a multi-dimensional array in both of our input values a1 and a2.

Make sure both the input arrays should be of the same dimension and same shapes. The numpy.add() function will find the Addition between array arguments, element-wise.

Can We Find Addition Between Two Numpy Arrays With Different Shapes?

In simple words, No, we can’t find addition or use the numpy add function in two numpy arrays that have different shapes.

Let’s look it through one example,

import numpy as np
a1 = [[20, 21, 5], [-9, 11, 1]]
a2 = [[7, 13, 17, 6], [1, -8, 7]]

print ("1st Input array : ", a1) 
print ("2nd Input array : ", a2) 
	
ad = np.add(a1, a2) 
print ("Addition of two input arrays : ", ad) 

Output:

ValueError: operands could not be broadcast together with shapes (2,3) (2,)

Explanation

If the shape of two numpy arrays will be different than we will get a value error. The value error will say something like for example.

ValueError: operands could not be broadcast together with shapes (2,3) (2,)

Here in this example, we get a value error because the a2 input array has a different shape than the a1 input array. In order to get the addition without any value error, make sure to check the shape of arrays.

What’s Next?

NumPy is very powerful and incredibly essential for information science in Python. That being true, if you are interested in data science in Python, you really ought to find out more about Python.

You might like our following tutorials on numpy.

Conclusion

The numpy add() is a compelling and essential function available in the numpy module, which can be very useful and highly recommended by many experts while finding the addition between very large data sets.

If you still have any questions regarding the NumPy add function. Leave your question in the comments below.

Happy Pythonning!

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments