Working with numpy.multiply() function (4 examples)

Updated: February 25, 2024 By: Guest Contributor Post a comment

Introduction

The numpy.multiply() function in Python’s NumPy library is a mathematical operation that performs element-wise multiplication on arrays. Its primary use is to multiply the contents of two arrays on a one-to-one basis. This tutorial explores how to use the numpy.multiply() function through four progressively advanced examples. Whether you’re just starting out with NumPy or looking to deepen your understanding, this guide provides a comprehensive walkthrough.

Syntax & Parameters

Before diving into the examples, it’s important to understand the syntax of numpy.multiply():

numpy.multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

The parameters are as follows:

  • x1, x2: Input arrays to be multiplied. These can be scalars or N-dimensional arrays.
  • out: A location into which the result is stored (optional).
  • where: A condition that selects which elements to multiply (optional).
  • casting, order, dtype, subok: These parameters control how the operation is performed and how the output is cast and stored.

Now, let’s proceed with our examples.

Example 1: Basic Array Multiplication

In this basic example, we multiply two 1-dimensional arrays together. This is a great starting point to understand how numpy.multiply() processes arrays.

import numpy as np

# Creating two arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Element-wise multiplication
result = np.multiply(a, b)

print(result)

The output is:

[ 4 10 18]

Here, each element in array a is multiplied by the corresponding element in array b, resulting in a new array where each position holds the product of the two input arrays’ elements.

Example 2: Scalar and Array Multiplication

In our second example, we demonstrate how a scalar value can be multiplied across each element of an array using numpy.multiply().

import numpy as np

# Creating an array
a = np.array([1, 2, 3, 4, 5])

# Scalar to multiply
scalar = 2

# Multiplying array by scalar
result = np.multiply(a, scalar)

print(result)

The output demonstrates the array elements all multiplied by the scalar value:

[ 2  4  6  8 10]

This example illustrates the flexibility of numpy.multiply() in handling operations between arrays and scalar values.

Example 3: Multi-dimensional Array Multiplication

Moving on to more complex structures, this example tackles multiplying multi-dimensional arrays.

import numpy as np

# Creating multi-dimensional arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

# Element-wise multiplication
result = np.multiply(a, b)

print(result)

The output:

[[ 5 12]
 [21 32]]

Here, numpy.multiply() performs an element-wise multiplication across the two 2D arrays, maintaining the structure and size of the input arrays.

Example 4: Broadcasting in Multiplication

NumPy’s broadcasting rules allow numpy.multiply() to multiply arrays of different sizes in a meaningful way. This final example demonstrates how broadcasting works in the context of array multiplication.

import numpy as np

# 1D and 2D array
a = np.array([1, 2, 3])
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Broadcasting and multiplying
result = np.multiply(a, b)

print(result)

The output shows how a is broadcast across each row of b before multiplication:

[[ 1  4  9]
 [ 4 10 18]
 [ 7 16 27]]

This example demonstrates the power of NumPy broadcasting in simplifying operations across arrays of different shapes.

Conclusion

Throughout these examples, we’ve seen the versatility and power of the numpy.multiply() function, from basic array multiplication to the complexities of broadcasting with multi-dimensional structures. Understanding how to effectively use this function can significantly enhance your data manipulation and mathematical operations within Python projects.