In this tutorial, I’ll explain how to use the Numpy multiply function – AKA np.multiply – to multiply matrices together. Show
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:
Ok. Let’s get to it. A Quick Introduction to Numpy MultiplyAs 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.multiplyThe syntax for the Numpy multiply function is simple: Remember that this syntax assumes that you’ve imported Numpy with the code Format of the input arraysNotice that there are two input arguments to the function, which I’ve named 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. Additional parametersIn 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.multiplyThe 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 togetherNow, let’s take a look at some examples. Examples:
Preliminary code: Import Numpy and Create ArraysBefore 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. Import NumpyFirst, 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 Create ArraysNext, we need to create some numpy arrays that we can operate on. Here, we’re going to create several Numpy objects:
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 scalarsFirst, we’ll start with the simplest case. Here, we’ll use np.multiply to multiply two scalar values. np.multiply(3,4) OUT: 12 ExplanationObviously, 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 scalarNext, we’re going to multiply a 2-dimensional Numpy array by a scalar (i.e., we’ll multiply a matrix by a scalar). np.multiply(matrix_2d_ordered, 2) OUT: array([[ 2, 4, 6], [ 8, 10, 12], [14, 16, 18]]) ExplanationSo what happened here? We called np.multiply with two arguments: the Numpy array For the output, np.multiply multiplied every value of It’s pretty straight forward. EXAMPLE 3: Multiply two same-sized Numpy arraysNow, let’s multiply two arrays with the same size. Here, we’re going to multiply np.multiply(matrix_2d_ordered, matrix_2d_random) OUT: array([[ 9, 4, 12], [12, 35, 6], [56, 40, 54]]) ExplanationHere, 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. np.multiply(matrix_2d_ordered, vector_1d) OUT: array([[ 10, 40, 90], [ 40, 100, 180], [ 70, 160, 270]]) ExplanationIn 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 belowDo 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 NumpyIn 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:
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: Learn More About Numpy Mastery How does Python multiply matrices?There are three main ways to perform NumPy matrix multiplication:. dot(array a, array b) : returns the scalar or dot product of two arrays.. matmul(array a, array b) : returns the matrix product of two arrays.. multiply(array a, array b) : returns the element-wise matrix multiplication of two arrays.. Can you multiply two arrays?C = A . * B multiplies arrays A and B by multiplying corresponding elements. The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.
How does NumPy multiply work?What does Numpy Multiply Function do? The numpy multiply function calculates the product between the two numpy arrays. It calculates the product between the two arrays, say x1 and x2, element-wise.
|