NumPy is one of the popular Python libraries that offers a wide variety of arithmetic and numerical functions. This article aims to clarify how to perform addition operations on NumPy arrays using the numpy.add() and addition operator. We will look at both ways in detail so that you can use the one that best suits you. Let’s get started.
Also Read: Numpy Broadcasting (With Examples)
Addition Using numpy.add() Function
numpy.add() is a function in the NumPy library that is designed for the element-wise addition of two arrays. It can also be used for scalar addition by providing a scalar value along with an array.
Below is the syntax for using numpy.add() function in Python.
Syntax:
numpy.add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
Parameters:
- x1, x2: The input arrays for addition.
- out (optional): The output array in which the result is placed.
- where (optional): A boolean array indicating where the operation should occur.
- casting (optional): Controls what kind of data casting may occur.
- order (optional): ‘K’, ‘A’, ‘C’, or ‘F’ to control the memory layout of the output.
- dtype (optional): Desired data type for the output.
- subok (optional): If True, then the input arrays are allowed to be subclasses.
Return: An NumPy array representing the sum of x1 and x2, element-wise.
Example 1: Adding Array with Scalar
We can add an array with any dimension to a scaler value using numpy.add().
import numpy as np
arr=np.array([1,7,6,4])
scValue=100
result= np.add(arr,scValue)
result
In the above code, we first imported the Numpy library as np then we created a 1D array and stored it into a variable arr. Then we declared a scaler value as 100 and stored it into variable scValue. After that, we provided an array with the scaler value in the numpy.add() function for addition and stored the result into a result variable, and at last, we printed the variable.
Output:
In the output above we have seen that we got an array after adding the single number or a scaler value with all the elements of the 1D array using numpy.add() function.
Example 2: Adding Two 1D Arrays
In this example, we will learn how to add two 1D arrays using numpy.add() function.
import numpy as np
ar1 = np.array([21,34,7])
ar2 = np.array([3,8,6])
result = np.add(ar1,ar2)
result
Here we created two 1D arrays and passed both the arrays into the numpy.add() function for adding them and save the result into a variable called result. In last, we printed the variable.
Output:
In the output, we have seen that the first element of the first array got added with the first element of the second array, and so on until all the elements got added between those arrays.
Example 3: Adding Two 2D Arrays
Likewise, with the addition of two 1D arrays, we will do the addition of two 2D arrays using numpy.add() function in this example.
import numpy as np
ar1 = np.array([[9, 8], [36, 5]])
ar2 = np.array([[3, 85], [2, 13]])
result= np.add(ar1,ar2)
result
Here also we created two arrays but both are 2D and then applied numpy.add() function on both the array and save the result into the variable result. Lastly, we printed the variable.
Output:
Above we can see the same thing as the last example, here also first element of the first array was added to the first element of the second element and it continued till the last element of both arrays.
Addition Using Addition Operator (+)
The addition operator also performs element-wise addition for NumPy arrays. It is basically used for basic operations. The result which we were getting with numpy.add() function for the addition, we can get the same result by using the addition operator. Let’s see a few examples.
Example 1: Adding Array with Scalar
Here also by using the additon operator, we can get the same result for adding a scaler value with the array that we did in the example of numpy.add() function
import numpy as np
ar=np.array([12,10,88,99])
scVal=4
result=ar+scVal
result
In the above code, we first imported the NumPy as np then we created an array using the NumPy library. Then we stored a scaler value 4 into the variable scVal. Then we used the addition operator for the addition this time and stored the result of addition into the result variable and then printed it.
Output:
Above we can see that the scaler value got added with each element from the array one by one like with the numpy.add() function.
Example 2: Adding Two 1D Arrays
We can also add two arrays by just using an addition operator between the two arrays.
import numpy as np
ar1 = np.array([15,16,13,14,15])
ar2 = np.array([4,5,6,7,8])
result= ar1 + ar2
result
Output:
In the same manner as the numpy.add() function, here also we have seen that the first element of the first array got added with the first element of the second array, and so on until all the elements got added between those arrays.
Example 3: Adding Two 2D Arrays
In the same way that we did for the 1D array, we can do it with the 2D array for addition using the addition operator.
import numpy as np
ar1 = np.array([[1,6],[2,4]])
ar2 = np.array([[3,8],[9,7]])
result= ar1 + ar2
result
Output:
Summary
In this tutorial, we have discussed numpy.add() function provided by Python’s NumPy library with the addition operator and explored three examples of both ways to find the addition of NumPy arrays. After reading this tutorial, we hope you can easily do the addition of NumPy arrays in Python.
Reference
https://numpy.org/doc/stable/reference/generated/numpy.add.html