Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn ufunc Product

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:


1. Element-wise Product (numpy.prod())This function computes the product of all elements in the input array or along a specified axis.

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]

Cumulative Product(numpy.cumprod()): :This function computes the cumulative product of elements in the input array along a specified axis.

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]


Row-wise or Column-wise Product (numpy.prod() with axis parameter): You can use numpy.prod() with the axis parameter to compute the product along a specific axis, either row-wise or column-wise.

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.






Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java