• Post author:
• Post category:NumPy / Python

The NumPy `multiply()` function can be used to compute the element-wise multiplication of two arrays with the same shape, as well as multiply an array with a single numeric value. This function provides several parameters that allow the user to specify what value to multiply with. When used with two arrays of the same shape,` numpy.multiply()` performs element-wise multiplication, meaning it multiplies the corresponding elements of the input arrays to produce an output array with the same shape.

Use `numpy.multiply()` function to multiplicate the first array elements (arr) with the second array elements (arr1). In this article, I will explain how to use the NumPy `multiply()` function and using it to returns an array that contains the multiplication of an input array.

## 1. Quick Examples of Element Wise Multiplication

If you are in a hurry, below are some quick examples of how to use NumPy element-wise multiplication.

``````
# Quick examples of element wise multiplication

# Example 1: Using multiply() function
# Get multiplication values
arr = [2, 4, 6, 8, 5, 7]
arr2 = np.multiply(arr,3)

# Example 2: Use numpy.multiply() function
# To multiplication two numbers
arr = 5
arr1 = 8
arr2 = np.multiply(arr, arr1)

# Example 3: Use numpy.multiply() function
arr2 = np.multiply(3, 9)

# Example 4: Use numpy.multiply()
# Get the multiplication
arr = np.array([2, 4, 6, 8, 1])
arr1 =np.array( [3, 5, 7, 9, 2])
arr2 = np.multiply(arr, arr1)

# Example 5: Use numpy.mutiply() function
# Get the matrix multiplication
arr = np.array([[2, 4, 6, 8],[1, 3, 5, 7]])
arr1 = np.array([[2, 3, 5, 4],[8, 5, 3, 2]])
arr2 = np.multiply(arr, arr1)

# Example 6: Get the certain rows multiplication
arr2 = np.multiply(arr[1,:],arr1[0,:])
arr3 = np.multiply(arr[ 0,: 2], arr1[ 1,: 1])
arr4 = np.multiply(arr[ 1,: 3], arr1[ 0,: 1])

# Example 7: Using the * operator
# Element-wise multiplication
arr = np.array([[2, 4, 6, 8],[7, 3, 5, 9]])
arr1 = np.array([[3, 7, 5, 4],[8, 6, 9, 2]])
arr2 = arr * arr1
``````

## 2. Syntax of multiply()

Following is the syntax of `numpy.multiply()` function.

``````
# Syntax of numpy.multiply()
numpy.multiply(arr, arr1, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) =
``````

### 2.1 Parameters of NumPy multiply()

Following are the parameters of multiply().

• `arr` – The first input array or object works as a multiplication.
• `arr1` – Second input array or object which works as a multiplication.
• `out` – It is ndarray, None, or tuple of ndarray and None, optional. Out will be the location where the result is to be stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly allocated array is returned.
• `where (optional)` – This parameter is used to specify conditions for element-wise multiplication. If where is True (default), perform the operation. If False, skip the operation and return the input array.
• `casting (optional)` – Controls what kind of data casting may occur during the operation. Default is ‘same_kind’.
• `order` – {‘C’, ‘F’, ‘A’, ‘K’}, optional: ‘C’: means to flatten in row-major using C-style order. ‘F’: means to flatten in column-major (Fortran- style) order. ‘A’: means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. K’: means to flatten an in the order the elements occur in memory. By default, the ‘C’ index order is used.
• `dtype` – It is an optional parameter that specifies the data type of the returned array.

### 2.2 Return value

It returns an array that contains the multiplication values of input arrays.

## 3. Usage of NumPy multiply() Function

The `numpy.multiply()` is a mathematical function and is used to calculate the multiplication between two NumPy arrays. Returns a multiplication of the inputs, element-wise.

You can multiply the array with a scalar value, to do so, you have taken an array named `arr` as a multiplicated and the scalar value `3` which indicates the multiplication. Now you will pass the array and scalar value as an argument in `numpy.multiply()` function. First, you have to create an NumPy array and then, apply this function.

``````
# Import numpy
import numpy as np

# Creating an 1D input array
arr = [2, 4, 6, 8, 5, 7]
print("Original array:\n",arr)

# Using multiply() function
# Get multiplication values
arr2 = np.multiply(arr,3)
print("After element-wise multiplication:\n",arr2)
``````

Yields below output.

## 4. Use numpy.multiply() Function To Multiplication Two Numbers

The `numpy.multiply()` function in NumPy is typically used for element-wise multiplication of arrays. If you want to multiply two scalar numbers, you can simply use the `*` operator in Python.

In the below example, the `*` operator is used to multiply `arr` and `arr1` directly, resulting in the product `40`. The `numpy.multiply()` function is not necessary for multiplying two scalar numbers, it is more suitable for element-wise multiplication of arrays.

``````
import numpy as np

# Initialize variables
arr = 5
arr1 = 8

# Using the * operator
# To multiply two numbers
arr2 = arr * arr1
print("After multiplying two numbers:\n",arr2)

# Use numpy.multiply() function
# To multiplication two numbers
arr2 = np.multiply(arr, arr1)
print("After multiplying two numbers:\n",arr2)
``````

Yields below output.

You can use `numpy.multiply()` function to multiply two scalar numbers as well. For example, you are using the `numpy.multiply()` function to multiply 3 and 9, and the result will be stored in the variable `arr2`. In this code, the `numpy.multiply()` function multiplies 3 and 9, and the output is `27`.

``````
# Use numpy.multiply() function
arr2 = np.multiply(3, 9)
print("After multiplying two numbers:\n",arr2)

# Output:
After multiplying two numbers:
27
``````

## 5. Multiplication NumPy Array Elementwise

Alternatively, you have created two NumPy arrays `arr` and `arr1`, and then used the `numpy.multiply()` function to perform element-wise multiplication between these two arrays. In this code, `numpy.multiply()` performs element-wise multiplication between `arr` and `arr1`, resulting in the array `[6, 20, 42, 72, 2]`.

``````
import numpy as np

# Creating two input array
arr = np.array([2, 4, 6, 8, 1])
arr1 =np.array( [3, 5, 7, 9, 2])

# Use numpy.multiply()
# Get the multiplication
arr2 = np.multiply(arr, arr1)
print("After element-wise multiplication:\n",arr2)

# Output:
# After element-wise multiplication:
#  [ 6 20 42 72  2]
``````

## 7. Use NumPy.multiply() with Two Dimension Arrays

You can use the `numpy.multiply()` function to perform element-wise multiplication with two-dimensional arrays. For instance, `numpy.multiply()` performs element-wise multiplication on the two 2D arrays `arr` and `arr1`, resulting in the output array `[[ 4 12 30 32], [ 8 15 15 14]]`. Each element in the output array is calculated by multiplying the corresponding elements from the input arrays.

``````
import numpy as np

# Create a numpy two dimensional arrays
arr = np.array([[2, 4, 6, 8],[1, 3, 5, 7]])
arr1 = np.array([[2, 3, 5, 4],[8, 5, 3, 2]])

# Use numpy.mutiply() function
# Get the matrix multiplication
arr2 = np.multiply(arr, arr1)
print("After element-wise multiplication:\n",arr2)

# Output:
# After element-wise multiplication:
#  [[ 4 12 30 32]
#  [ 8 15 15 14]]
``````

To pass certain rows, columns, or submatrices to the `numpy.multiply()` function and get the multiplication of certain rows, columns, and submatrices. You should follow the same sizes of the rows, columns, or submatrices that you pass as our operands. Let’s take an example,

``````
# Get the certain rows multiplication
arr2 = np.multiply(arr[1,:],arr1[0,:])
print(arr2)

# Output:
# [ 2  9 25 28]

arr3 = np.multiply(arr[ 0,: 2], arr1[ 1,: 1])
print(arr3)

# Output:
# [16 32]

arr4 = np.multiply(arr[ 1,: 3], arr1[ 0,: 1])
print(arr4)

# Output:
# [ 2  6 10]
``````

## 8. Multiplication NumPy Arrays With the * Operator

You can multiply NumPy arrays element-wise using the `*` operator. When you use the `*` operator between two NumPy arrays, it performs element-wise multiplication.

In the below example, the `*` operator performs element-wise multiplication on the two 2D arrays `arr` and `arr1`, resulting in the output array `[[ 6 28 30 32], [56 18 45 18]]`. Each element in the output array is calculated by multiplying the corresponding elements from the input arrays.

``````
import numpy as np

# Create a numpy two dimensional arrays
arr = np.array([[2, 4, 6, 8],[7, 3, 5, 9]])
arr1 = np.array([[3, 7, 5, 4],[8, 6, 9, 2]])

# Using the * operator
# Element-wise multiplication
arr2 = arr * arr1
print("After element-wise multiplication:\n",arr2)

# Output:
# After element-wise multiplication:
#  [[ 6 28 30 32]
#  [56 18 45 18]]
``````

What is element-wise multiplication in NumPy?

Element-wise multiplication in NumPy refers to the operation where corresponding elements of two arrays are multiplied together to create a new array. It is performed using the `*` operator or the `numpy.multiply()` function. In element-wise multiplication, each element in the resulting array is calculated by multiplying the corresponding elements from the input arrays.

How can I perform element-wise multiplication in NumPy using the * operator?

You can perform element-wise multiplication in NumPy using the `*` operator. When you use the `*` operator between two NumPy arrays, it performs element-wise multiplication.

Is element-wise multiplication a commutative operation in NumPy?

Element-wise multiplication is a commutative operation in NumPy, which means the order of the operands does not affect the result. When you perform element-wise multiplication on two NumPy arrays using the `*` operator or the `numpy.multiply()` function, the result will be the same regardless of the order of the operands.

What happens if I multiply a NumPy array by a scalar value?

When you multiply a NumPy array by a scalar value, each element in the array is multiplied by that scalar. This operation is performed element-wise, meaning each element in the array is multiplied individually.

Can I use the numpy.multiply() function for element-wise multiplication of arrays with different shapes?

The `numpy.multiply()` function requires input arrays to have the same shape for element-wise multiplication. If you have arrays with different shapes, you need to ensure they are compatible according to NumPy’s broadcasting rules before performing element-wise multiplication.

Can I perform element-wise multiplication on multi-dimensional arrays in NumPy?

You can perform element-wise multiplication on multi-dimensional arrays in NumPy. When using the `*` operator or the `numpy.multiply()` function with multi-dimensional arrays, it performs element-wise multiplication on corresponding elements of the arrays. Ensure that the arrays have compatible shapes according to NumPy’s broadcasting rules.

## Conclusion

In this article, I have explained how to use `numpy.multiply()` function that performs element-wise multiplication between two arrays in NumPy. It can be used to multiply 1-D arrays, 2-D arrays, or even multi-dimensional arrays. When using `numpy.multiply()`, ensure that the input arrays have compatible shapes according to NumPy’s broadcasting rules.

Happy Learning!!

## References

### Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.