In this tutorial, I’ll explain how to use the Numpy multiply function – AKA np.multiply – to multiply matrices together.
I’ll explain the syntax of np.multiply, how the function works, and how to use it.
If you need something specific, you can click on any of the following links.
Table of Contents:
- Introduction to Numpy Multiply
- The syntax of np.multiply
- Examples: how to multiply matrices together
Ok. Let’s get to it.
A Quick Introduction to Numpy Multiply
As you might have guessed, the Numpy multiply function multiplies matrices together.
You can use np.multiply to multiply two same-sized arrays together. This computes something called the Hadamard product. In the Hadamard product, the two inputs have the same shape, and the output contains the element-wise product of each of the input values.
You can also use np.multiply to multiply a matrix by a vector. If you multiply a matrix by a vector (e.g., a multi-dimensional array by a lower-dimensional array), Numpy will perform broadcasting.
Both techniques are pretty simple, and I’ll show you examples of both.
But first, let’s take a look at the syntax.
The syntax of np.multiply
The syntax for the Numpy multiply function is simple:
Remember that this syntax assumes that you’ve imported Numpy with the code
import numpy as np.
Format of the input arrays
Notice that there are two input arguments to the function, which I’ve named
arr1. These should be Numpy arrays or array-like objects such as Python lists.
Also, there are some restrictions on the shape of the input array.
One way to use np.multiply, is to have the two input arrays be the exact same shape (i.e., they have the same number of rows and columns). If the input arrays have the same shape, then the Numpy multiply function will multiply the values of the inputs pairwise.
Alternatively, if the two input arrays are not the same size, then one of the arrays must have a shape that can be broadcasted across the other array.
Broadcasing is somewhat complicated to understand if you’re a Numpy beginner, so I’ll show you an example in the examples section.
In addition to the two input arguments, there are a few optional parameters for the Numpy multiply function:
These are somewhat rarely used, but so I won’t explain them here.
Output of np.multiply
The output of np.multiply is a new Numpy array that contains the element-wise product of the input arrays.
Having said that, there is a special case for scalars: if both inputs to np.multiply are scalar values, then the output will be a scalar.
Examples: how to calculate multiply Numpy arrays together
Now, let’s take a look at some examples.
- Use Numpy multiply on two scalars
- Use Numpy multiply with one array and one scalar
- Multiply two same-sized Numpy arrays
- Multiply differently sized Numpy arrays with broadcasting (i.e., multiply a matrix by a vector)
Preliminary code: Import Numpy and Create Arrays
Before you run any of the examples, you’ll need to run some preliminary code.
Specifically, before you run any of the examples, you’ll need to import Numpy and you’ll need to create some Numpy arrays that we can work with.
First, we nee to import Numpy before we can use any of the Numpy functions.
You can do that with the following code:
import numpy as np
Next, we need to create some numpy arrays that we can operate on.
Here, we’re going to create several Numpy objects:
- a 1-dimensional ‘vector’ of numbers
- a 2-dimensional ‘matrix’ of the numbers from 1 to 9 (ordered)
- a 2-dimensional ‘matrix’ of the numbers from 1 to 9 (randomized)
To do this, we’ll use a few Numpy functions, like the Numpy array function, the Numpy arange function, the Numpy reshape method, and Numpy random choice. (If you’re unsure about what we’re doing here, you should read those other tutorials.)
# CREATE 1D 'VECTOR' vector_1d = np.array([10,20,30]) # CREATE 2D MATRIX OF NUMBERS, 1 TO 9 numbers_1_to_9 = np.arange(start = 1, stop = 10) matrix_2d_ordered = numbers_1_to_9.reshape((3,3)) # CREATE 2D MATRIX OF NUMBERS, 1 TO 9, RANDOMIZED np.random.seed(22) matrix_2d_random = np.random.choice(size = (3,3), a = numbers_1_to_9, replace = False)
Once you have Numpy imported and once you’ve created the arrays, you’ll be ready to run the examples.
EXAMPLE 1: Use the Numpy multiply on two scalars
First, we’ll start with the simplest case.
Here, we’ll use np.multiply to multiply two scalar values.
Obviously, this is very simple and straight forward.
Here, we’re simply multiplying 3 times 4. The result is 12.
EXAMPLE 2: Multiply an array by a scalar
Next, we’re going to multiply a 2-dimensional Numpy array by a scalar (i.e., we’ll multiply a matrix by a scalar).
array([[ 2, 4, 6], [ 8, 10, 12], [14, 16, 18]])
So what happened here?
We called np.multiply with two arguments: the Numpy array
matrix_2d_ordered and the scalar value 2.
For the output, np.multiply multiplied every value of
matrix_2d_ordered by 2.
It’s pretty straight forward.
EXAMPLE 3: Multiply two same-sized Numpy arrays
Now, let’s multiply two arrays with the same size.
Here, we’re going to multiply
array([[ 9, 4, 12], [12, 35, 6], [56, 40, 54]])
Here, np.multiply is multiplying together the values of each input matrix, element-wise.
The output is a matrix of the same size as the inputs, that contains the element wise product of the values of the input matrices.
(This is known as the Hadamard product.)
EXAMPLE 4: Multiply a matrix by a vector (i.e., broadcasting)
Finally, let’s do one more example.
Here, we’re going to multiply one of our 2-dimensional input arrays by a 1-dimensional array. Effectively, this is like multiplying a matrix by a vector.
array([[ 10, 40, 90], [ 40, 100, 180], [ 70, 160, 270]])
In this example, we multiplied a 2-dimensional matrix by a 1-dimensional vector. (I.e., we multiplied a 2D Numpy a 1D Numpy array).
When we do this, Numpy performs what is called “broadcasting.” Effectively, it takes the 1D vector, treats it as a row of data, and multiplies that vector by every row in the 2D array.
So it multiplies row 1 of the matrix by the vector, element wise. Then it multiplies row 2 of the matrix by the vector. And so on.
Keep in mind that when you do this, vector must have a shape such that it can be broadcast over the matrix.
Leave your other questions in the comments below
Do you have other questions about how to multiply Numpy arrays? Do you have questions about how to multiply matrices and vectors in Numpy?
If so, leave your questions in the comments section below.
Join our course to learn more about Numpy
In this tutorial, I’ve explained how to multiply arrays in Numpy with np.multiply.
This should help you with array multiplication, but if you really want to learn Numpy, there’s a lot more to learn.
If you’re serious about mastering Numpy, and serious about data science in Python, you should consider joining our premium course called Numpy Mastery.
Numpy Mastery will teach you everything you need to know about Numpy, including:
- How to create Numpy arrays
- How to reshape, split, and combine your Numpy arrays
- What the “Numpy random seed” function does
- How to use the Numpy random functions
- How to perform mathematical operations on Numpy arrays
- and more …
Moreover, this course will show you a practice system that will help you master the syntax within a few weeks. We’ll show you a practice system that will enable you to memorize all of the Numpy syntax you learn. If you have trouble remembering Numpy syntax, this is the course you’ve been looking for.
Find out more here: