Warning: preg_match_all(): Compilation failed: quantifier does not follow a repeatable item at offset 24 in /home/u681382254/domains/machinelearningpy.com/public_html/wp-content/plugins/seo-by-rank-math/includes/replace-variables/class-post-variables.php on line 545

Warning: preg_match_all(): Compilation failed: quantifier does not follow a repeatable item at offset 24 in /home/u681382254/domains/machinelearningpy.com/public_html/wp-content/plugins/seo-by-rank-math/includes/replace-variables/class-post-variables.php on line 545
Add Arrays with (+) and Numpy add() Function [6 Examples] - Machine Learning PY
Warning: preg_match_all(): Compilation failed: quantifier does not follow a repeatable item at offset 24 in /home/u681382254/domains/machinelearningpy.com/public_html/wp-content/plugins/seo-by-rank-math/includes/replace-variables/class-post-variables.php on line 545

Warning: preg_match_all(): Compilation failed: quantifier does not follow a repeatable item at offset 24 in /home/u681382254/domains/machinelearningpy.com/public_html/wp-content/plugins/seo-by-rank-math/includes/replace-variables/class-post-variables.php on line 545

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:

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)

#### Output

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.

#### Note

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)

#### Output

[[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)

#### Output

[10 74 84 94]
The addition of a single item to all the items of the array can be done using the addition operator and its practical implementation can be seen in the above example code.

a1 = np.array([11,33,77])
a2 = np.array([43,7,6])

print(result)

#### Output

[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.

firstArr2D = np.array([[9, 8], [36, 5]])
secondArr2D = np.array([[3, 685], [2, 213]])

print(resArray)

#### Output

[[ 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