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.
Add two numpy arrays
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 –
Add two 1d 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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
Add two 2d arrays elementwise
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]]
Add more than two arrays elementwise
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.
What if the arrays have different dimensions?
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.
Tutorials on numpy arrays –
- How to sort a Numpy Array?
- Create Pandas DataFrame from a Numpy Array
- Different ways to Create NumPy Arrays
- Convert Numpy array to a List – With Examples
- Append Values to a Numpy Array
- Find Index of Element in Numpy Array
- Read CSV file as NumPy Array
- Filter a Numpy Array – With Examples
- Python – Randomly select value from a list
- Numpy – Sum of Values in Array
- Numpy – Elementwise sum of two arrays
- Numpy – Elementwise multiplication of two arrays
- Using the numpy linspace() method
- Using numpy vstack() to vertically stack arrays
- Numpy logspace() – Usage and Examples
- Using the numpy arange() method
- Using numpy hstack() to horizontally stack arrays
- Trim zeros from a numpy array in Python
- Get unique values and counts in a numpy array
- Horizontally split numpy array with hsplit()