Elementwise sum of a 2d numpy array

Numpy – Elementwise sum of two arrays

In this tutorial, we will look at how to get a numpy array resulting from the elementwise sum of two numpy arrays of the same dimensions.

Elementwise sum of a 2d numpy array

You can use the numpy np.add() function to get the elementwise sum of two numpy arrays. The + operator can also be used as a shorthand for applying np.add() on numpy arrays. The following is the syntax:

import numpy as np
# x1 and x2 are numpy arrays of same dimensions
# using np.add()
x3 = np.add(x1, x2)
# using + operator
x3 = x1 + x2

It returns a numpy array resulting from the elementwise addition of each array value.

Let’s look at some examples of adding numpy arrays elementwise –

To elementwise add two 1d arrays, pass the two arrays as arguments to the np.add() function. Let’s show this with an example.

import numpy as np

# create numpy arrays x1 and x2
x1 = np.array([1, 3, 0, 7])
x2 = np.array([2, 0, 1, 1])
# elementwise sum with np.add()
x3 = np.add(x1, x2)
# display the arrays
print("x1:", x1)
print("x2:", x2)
print("x3:", x3)

Output:

x1: [1 3 0 7]
x2: [2 0 1 1]
x3: [3 3 1 8]

The array x3 is the result of the elementwise summation of values in the arrays x1 and x2.

Alternatively, you can also use the + operator to add numpy arrays elementwise.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# elementwise sum with + operator
x3 = x1 + x2
# display the arrays
print("x1:", x1)
print("x2:", x2)
print("x3:", x3)

Output:

x1: [1 3 0 7]
x2: [2 0 1 1]
x3: [3 3 1 8]

You can see that we get the same results as above with x3 as the array resulting from the elementwise sum of arrays x1 and x2.

The syntax for adding higher-dimensional arrays is also the same. Pass the two arrays to the np.add() function which then returns a numpy array resulting from elementwise addition of the values in the passed arrays.

# create 2d arrays x1 and x2
x1 = np.array([[1, 0, 1],
               [2, 1, 1],
               [3, 0, 3]])
x2 = np.array([[2, 2, 0],
               [1, 0, 1],
               [0, 1, 0]])

# elementwise sum with np.add()
x3 = np.add(x1, x2)
# display the arrays
print("x1:\n", x1)
print("x2:\n", x2)
print("x3:\n", x3)

Output:

x1:
 [[1 0 1]
 [2 1 1]
 [3 0 3]]
x2:
 [[2 2 0]
 [1 0 1]
 [0 1 0]]
x3:
 [[3 2 1]
 [3 1 2]
 [3 1 3]]

Here, we add two 3×3 numpy arrays. The values in the array x3 are the result of the elementwise sum of values in the arrays x1 and x2.

Again, you can also use the + operator to perform the same operation.

# elementwise sum with + opeartor
x3 = np.add(x1, x2)
# display the arrays
print("x1:\n", x1)
print("x2:\n", x2)
print("x3:\n", x3)

Output:

x1:
 [[1 0 1]
 [2 1 1]
 [3 0 3]]
x2:
 [[2 2 0]
 [1 0 1]
 [0 1 0]]
x3:
 [[3 2 1]
 [3 1 2]
 [3 1 3]]

You can use the + operator to add (elementwise) more than two arrays as well. For example, let’s add three 1d arrays elementwise.

# create numpy arrays x1, x2, and x3
x1 = np.array([1, 3, 0, 7])
x2 = np.array([2, 0, 1, 1])
x3 = np.array([0, 1, 3, 1])
# elementwise sum with +
x4 = x1+x2+x3
# display the arrays
print("x1:", x1)
print("x2:", x2)
print("x3:", x3)
print("x4:", x4)

Output:

x1: [1 3 0 7]
x2: [2 0 1 1]
x3: [0 1 3 1]
x4: [3 4 4 9]

Here, the array x4 is the result of the elementwise sum of the arrays x1, x2, and x3.

Let’s see for ourselves.

# add two arrays with different dimensions
x1 = np.array([1, 3, 0, 7])
x2 = np.array([2, 0, 1, 1, 1])
x3 = np.add(x1, x2)

Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-cdaba2079557> in <module>
      2 x1 = np.array([1, 3, 0, 7])
      3 x2 = np.array([2, 0, 1, 1, 1])
----> 4 x3 = np.add(x1, x2)

ValueError: operands could not be broadcast together with shapes (4,) (5,) 

Trying to add two numpy arrays of different dimensions results in an error. This is because it doesn’t make sense to elementwise add two arrays that don’t have the same dimensions.

For more on the numpy np.add() function, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top