. Advertisement .
. Advertisement .
You can use the function multiply() to carry out an element-wise multiplication in NumPy. This tutorial will show you can accomplish this math task.
Note: this isn’t the same as matrix multiplication, which you can learn more from this guide.
Element-Wise Multiplication In NumPy
Most of the time, we only need to pay attention to two arguments – the two arrays we want to multiply. However, the syntax of the function
multiply() is actually a little more complicated with other parameters. This is the full syntax:
numpy.multiply(x1, x2, out, where)
x1 and x2: two arrays (or array-like objects) the function needs to multiply. If the shapes of these two arrays aren’t identical, they will be “broadcasted” to have compatible shapes.
out: the output object where the result of the multiplication is going to be stored.
This object must have the same shape as the two input objects. If you don’t provide it or set it to None, NumPy will allocate a wholly new array to store the result and return it. You can also specify a tuple of arrays, but its length and the number of outputs have to be equal. Its default value is None.
where: An array-like parameter specifies the condition that NumPy will broadcast over the input objects. Where this condition is false, the output array will keep the original value. Elsewhere, it will be set to the ufunc result. In the case the parameter out is set to none, entries where the condition is false won’t be uninitialized.
You can use the
* operator if your two input objects are both NumPy arrays (ndarrays).
With the first example, we are going to use two 3×3 arrays containing elements from 1 to 9 in random order.
import numpy as np x1 = np.array([ [6, 3, 9], [1, 8, 4], [7, 2, 5] ]) x2 = np.array([ [5, 2, 1], [9, 4, 8], [7, 6, 3] ])
To multiply them element-wise, just use them as the arguments for the function
>>> np.multiply(x1, x2) array([[30, 6, 9], [ 9, 32, 32], [49, 12, 15]])
The output (which is also known as the Hadamard product) is quite self-explanatory. The returned object is also a 3×3 NumPy array whose each element is the product of the corresponding elements in the original arrays.
You can produce the same result with the
* operator, which is a shorthand of the function multiply() when applied to two NumPy arrays:
>>> x1 * x2 array([[30, 6, 9], [ 9, 32, 32], [49, 12, 15]])
You can verify the type and shape of the returned object:
>>> x3 = np.multiply(x1, x2) >>> type(x3) <class 'numpy.ndarray'> >>> x3.shape (3, 3)
Using row and column indexing in NumPy, you can also perform this multiplication on only a subset of two arrays. For instance, this statement will only take the first two rows of the original arrays as the operands:
>>> np.multiply(x1[0:2], x2[0:2]) array([[30, 6, 9], [ 9, 32, 32]])
As you can see, the output is only a 3×2 array, which is also the first two rows of the full product when the whole array is used.
In a similar manner, this statement performs the element-wise multiplication on the last two columns of the arrays:
>>> np.multiply(x1[:, 1:3], x2[:, 1:3]) array([[ 6, 9], [32, 32], [12, 15]])
As expected, the result is the final two columns of the product in the first example.
multiply() is made for performing an element-wise multiplication in NumPy. It produces a similar result to the Hadamard product in linear algebra. Keep in mind that you will need to provide arrays of the same shape or at least objects NumPy can broadcast into compatible shapes.