In NumPy, product functions are included in the category of universal functions (ufuncs) and are used to compute the product of elements in a NumPy array along specified axes. NumPy provides several product functions to calculate different types of products:
import numpy as np
arr = np.array([[1, 2], [3, 4]])
# Compute product of all elements
total_product = np.prod(arr)
# Compute product along rows (axis=0)
row_product = np.prod(arr, axis=0)
# Compute product along columns (axis=1)
col_product = np.prod(arr, axis=1)
print("Total product:", total_product) # Output: 24
print("Product along rows:", row_product) # Output: [3 8]
print("Product along columns:", col_product) # Output: [ 2 12]
import numpy as np
arr = np.array([1, 2, 3, 4])
# Compute cumulative product
cum_product = np.cumprod(arr)
print("Cumulative product:", cum_product) # Output: [ 1 2 6 24]
import numpy as np
arr = np.array([1, 2, 3, 4])
# Compute cumulative sum
cum_sum = np.cumsum(arr)
print("Cumulative sum:", cum_sum) # Output: [ 1 3 6 10]
import numpy as np
arr = np.array([[1, 2], [3, 4]])
# Compute product along rows (axis=0)
row_product = np.prod(arr, axis=0)
# Compute product along columns (axis=1)
col_product = np.prod(arr, axis=1)
print("Product along rows:", row_product) # Output: [3 8]
print("Product along columns:", col_product) # Output: [ 2 12]
These product functions are useful for various mathematical and statistical computations, such as computing total values, factorial, and cumulative products. They provide efficient and vectorized operations for working with large arrays of data in NumPy.