NumPy - Product Universal Function (ufunc)



Product Universal Function (ufunc)

A product universal function (ufunc) in NumPy is a function used to compute the product of elements in an array.

This operation multiplies all the elements together, either for the entire array or along a specific axis (such as rows or columns). The primary product ufunc in NumPy is numpy.prod() function.

NumPy Product

The numpy.prod() function is used to compute the product of array elements over a specified axis. It can compute the product of all elements in an array or along a specific axis (e.g., row-wise or column-wise).

Example

In the following example, we use the numpy.prod() function to calculate the product of elements in an array −

import numpy as np

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

# Compute the product of all elements
total_product = np.prod(a)

# Compute the product along the columns
column_product = np.prod(a, axis=0)

# Compute the product along the rows
row_product = np.prod(a, axis=1)

print("Total product:", total_product)
print("Column-wise product:", column_product)
print("Row-wise product:", row_product)

Following is the output obtained −

Total product: 720
Column-wise product: [ 4 10 18]
Row-wise product: [ 6 120]

NumPy Cumulative Product

The numpy.cumprod() function is used to compute the cumulative product of array elements along a specified axis. It returns an array where each element is the cumulative product of the previous elements.

Example

In the following example, we use the numpy.cumprod() function to calculate the cumulative product of elements in an array −

import numpy as np

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

# Compute the cumulative product
cumulative_product = np.cumprod(a)

print("Cumulative product:", cumulative_product)

This will produce the following result −

Cumulative product: [  1   2   6  24 120]

NumPy Product with Conditions

The numpy.prod() function can also be used with conditional statements to compute the product of elements that meet a specific condition.

Example

In the following example, we use the numpy.prod() function to calculate the product of elements that are greater than a specified value −

import numpy as np

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

# Compute the product of elements greater than 2
conditional_product = np.prod(a[a > 2])

print("Product of elements greater than 2:", conditional_product)

The result produced is as follows −

Product of elements greater than 2: 60

Matrix Product with NumPy ufuncs

The matrix product in NumPy refers to multiplying two matrices together, following the rules of linear algebra. This operation is done using numpy.matmul() function or the @ operator, which computes the dot product of two arrays.

Example

In this example, np.matmul() function performs the matrix multiplication of matrix1 and matrix2, resulting in a new matrix −

import numpy as np

# Define two 2D arrays (matrices)
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

# Perform matrix multiplication
result = np.matmul(matrix1, matrix2)

print(result)

We get the output as shown below −

[[19 22]
 [43 50]]

NumPy Dot Product and Cross Product

The dot product calculates the sum of the products of corresponding elements in two arrays, while the cross product finds a vector perpendicular to two input vectors in 3D space.

NumPy provides numpy.dot() function for the dot product and numpy.cross() function for the cross product.

Example

In this example, np.dot() function calculates the dot product of the two vectors vector1 and vector2

import numpy as np

# Define two 1D arrays (vectors)
vector1 = np.array([1, 2, 3])
vector2 = np.array([4, 5, 6])

# Compute the dot product
dot_result = np.dot(vector1, vector2)

print(dot_result)

Following is the output obtained −

32

NumPy Element-wise Product Operations

Element-wise product operations in NumPy involve multiplying corresponding elements of two arrays. This is done using the numpy.multiply() function or the * operator, and it is useful for operations like scaling values in an array.

Example

In the following example, the np.multiply() function multiplies corresponding elements of array1 and array2 element-wise −

import numpy as np

# Define two arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Perform element-wise multiplication
product = np.multiply(array1, array2)

print(product)

This will produce the following result −

[ 4 10 18]
Advertisements