In this article, we’ll go through multiple ways through which the addition of single/multiple dimensions arrays can be done. Also, we’ll learn how to add a single item to all the items inside the array i.e. broadcasting. All these explanations will be practically implemented using Python code examples.
2 Methods to Add Arrays Items
There are 2 methods through which we can accomplish the action of adding Numpy arrays with each other. These are:
- Addition operator(+)
- Add function of Python Numpy Library(numpy.add())
Adding Arrays using Addition Operator (+)
Let’s add single and multi-dimensional arrays practically with code examples using the addition operator.
Example 1: Adding two 1D arrays
import numpy as np ar1 = np.array([1,2,3,4,5]) ar2 = np.array([4,5,6,7,8]) res_ar = ar1 + ar2 print("Result of adding arrays using + operator:", res_ar)
Result of adding arrays using + operator: [ 5 7 9 11 13]
- In this simple code example, we’ve imported the numpy library just to use its array function to create arrays.
- We’ve created two 1D(one-dimensional) arrays.
- Then we added both arrays using the addition(+) operator which can be seen in the above code. Also, we stored the resulting array inside a variable.
- Finally, after printing the value of the result. We can see that all the items of arrays are added. For instance, the first item of the first array is added to the first item of the second array, the second item of the first array is added to the second item of the second array, and so on until all the items are covered.
Please keep in mind that use the arrays with the same shape or it’ll raise an error. For instance, if the first array has 6 items, then the second array must have 6 items only.
Example 2: Addition of two 2D(two-dimensional) arrays
ar1twoD = np.array([[5,4],[2,5]]) ar2twoD = np.array([[9,8],[6,7]]) res_2D = ar1twoD + ar2twoD print( res_2D)
[[14 12] [ 8 12]]
We’ve created two 2D arrays. Then we used them as operands to the (+) operator, stored the resulting value inside a variable, and prints it. The output shows that the items of 2D arrays are successfully added and the resulting array has been returned.
Example 3: Add array items to a Single Item
ar=np.array([6,70,80,90]) scVal=4 res=ar+scVal print(res)
[10 74 84 94]
Add Arrays using Numpy add() Function
Let’s now use add() function of Python Numpy to add arrays.
Example 1: Adding two 1D Arrays using numpy.add()
a1 = np.array([11,33,77]) a2 = np.array([43,7,6]) result = np.add(a1,a2) print(result)
[54 40 83]
In this Python code:
We first generated two 1D arrays.
Then we passed these 2 arrays as arguments to the add function of the Numpy library. We store the resulting value inside a variable and displayed its value using the print function.
The result shows the same output as done by the addition operator. The first item of the first array is added to the second array’s first item and so on until all the items are added to both these arrays.
Example 2: numpy.add() to Add two 2D Arrays
firstArr2D = np.array([[9, 8], [36, 5]]) secondArr2D = np.array([[3, 685], [2, 213]]) resArray= np.add(firstArr2D,secondArr2D) print(resArray)
[[ 12 693] [ 38 218]]
This code shows the addition of two 2D arrays and the resulting array is returned, stored, and displayed.
Example 3: Numpy Array Broadcast (add a single item to all array items)
arr=np.array([3,7,10,40]) scValue=8 resultingArray= np.add(arr,scValue) print(resultingArray)
[11 15 18 48]
The addition of a single number to all the items of the Numpy array is done using the add() function of Numpy. The reason is that broadcasting of the scalar value to match the array’s shape is done by Numpy in order to perform element-wise addition.
In conclusion, we’ve learned two different methods through which we can add Numpy arrays. These were the addition(+) operator and the Numpy add function. Multiple examples have been included in this article to practically explain the implementation of both these methods in order to add 2 arrays or add scalar value to an array i.e. broadcasting.
Thank you for reading it.