In this tutorial, we will look at how to perform elementwise multiplication of two numpy arrays with the help of some examples.
Multiply two numpy arrays
You can use the numpy np.multiply()
function to perform the elementwise multiplication of two arrays. You can also use the *
operator as a shorthand for np.multiply()
on numpy arrays. The following is the syntax:
import numpy as np # x1 and x2 are numpy arrays of the same dimensions # elementwise multiplication x3 = np.multiply(x1, x2) # elementwise multiplication using * x3 = x1 * x2
It returns a numpy array of the same shape with values resulting from multiplying values in each array elementwise. Note that both the arrays need to have the same dimensions.
Let’s look at some examples –
Elementwise multiply two 1d arrays
import numpy as np # create two 1d numpy arrays x1 = np.array([1, 2, 0, 5]) x2 = np.array([3, 1, 7, 1]) # multiply x1 and x2 elementwise x3 = np.multiply(x1, x2) # display the arrays print("x1:", x1) print("x2:", x2) print("x3:", x3)
Output:
x1: [1 2 0 5] x2: [3 1 7 1] x3: [3 2 0 5]
Here, we created two one-dimensional numpy arrays of the same shape and then performed an elementwise multiplication. You can see that the resulting array, x3 has values resulting from the elementwise multiplication of values in x1 and x2.
Alternatively, you can also use the *
operator to perform the same elementwise multiplication operation.
# multiply x1 and x2 elementwise using * x3 = x1 * x2 # display the arrays print("x1:", x1) print("x2:", x2) print("x3:", x3)
Output:
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.
x1: [1 2 0 5] x2: [3 1 7 1] x3: [3 2 0 5]
We get the same results as above.
Elementwise multiply two 2d arrays
You can also perform this operation on higher-dimensional arrays. For example, let’s multiply two 2d numpy arrays elementwise.
# create two 2d numpy arrays x1 = np.array([[1, 2], [0, 5]]) x2 = np.array([[3, 1], [7, 1]]) # multiply x1 and x2 elementwise x3 = np.multiply(x1, x2) # display the arrays print("x1:\n", x1) print("x2:\n", x2) print("x3:\n", x3)
Output:
x1: [[1 2] [0 5]] x2: [[3 1] [7 1]] x3: [[3 2] [0 5]]
Here, we created two 2d (2×2) numpy arrays and then performed an elementwise multiplication on their values. You can see that we get a 2d array with values resulting from an elementwise multiplication of the values in the arrays x1 and x2.
Now let’s do the same operation but using the *
operator on arrays x1 and x2 –
# multiply x1 and x2 elementwise using * x3 = x1 * x2 # display the arrays print("x1:\n", x1) print("x2:\n", x2) print("x3:\n", x3)
Output:
x1: [[1 2] [0 5]] x2: [[3 1] [7 1]] x3: [[3 2] [0 5]]
We get the same results as above.
What if arrays are of different dimensions?
Let’s find out what happens if we use np.multiply()
on two numpy arrays with different dimensions.
# create two 1d numpy arrays x1 = np.array([1, 2, 0, 5]) x2 = np.array([3, 1, 7, 1, 6]) # multiply x1 and x2 elementwise x3 = np.multiply(x1, x2)
Output:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-3-3682bf62dbfa> in <module> 3 x2 = np.array([3, 1, 7, 1, 6]) 4 # multiply x1 and x2 elementwise ----> 5 x3 = np.multiply(x1, x2) ValueError: operands could not be broadcast together with shapes (4,) (5,)
You can see that we get an error. This happened because an elementwise operation requires the two arrays to have the same dimensions.
For more on the numpy np.multiply() 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()