From ed7c7326dfe45e085315323ce99e89b7a80f9b87 Mon Sep 17 00:00:00 2001 From: Vanshika Date: Tue, 28 Oct 2025 15:49:01 +0530 Subject: [PATCH 1/3] TermEntryNdarrayProd #7863 --- .../numpy/concepts/ndarray/terms/prod/prod.md | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 content/numpy/concepts/ndarray/terms/prod/prod.md diff --git a/content/numpy/concepts/ndarray/terms/prod/prod.md b/content/numpy/concepts/ndarray/terms/prod/prod.md new file mode 100644 index 00000000000..08afe69d8e1 --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/prod/prod.md @@ -0,0 +1,77 @@ +--- +Title: '.prod()' +Description: 'Calculates the product of all elements in a NumPy array, optionally along a specified axis.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Arrays' + - 'Mathematical Functions' + - 'Functions' + - 'NumPy' +CatalogContent: + - 'learn-python-3' + - 'paths/data-science' +--- + +The **`.prod()`** function in NumPy computes the product (multiplication) of all the elements in the array. This function is essential for mathematical and statistical operations and is particularly useful with multi-dimensional arrays where you might need to calculate the product across specific dimensions (rows or columns). The function is also available as the standalone function **`numpy.prod()`**. +## Syntax + +```psuedo +ndarray.prod(axis=None, dtype=None, out=None, keepdims=False, initial=1, where=True) +``` + +- `axis`: Optional. The axis or axes along which the product is computed. + If None (default), the product of all elements (the array is effectively flattened) is returned. + If an int (e.g., 0 or 1)the product is calculated along that axis. +- `dtype`:The data type of the returned product. By default, it's often inferred to prevent intermediate overflow, typically using the platform's default integer or float. +- `out`: Alternative output array in which to place the result. +- `keepdims `:If True, the reduced axis is retained in the result with a dimension size of 1. +- `initial `:The starting value for the product, which is multiplied by the product of the array elements. The default is 1. +- `where`:Elements corresponding to False in this array are excluded from the product. + +## Example + +The following example shows the use of the `.prod()`to calculate the product of all elements in a 1D array and the product along the columns in a 2D array: + + +```py +import numpy as np + +# 1D array example +arr_1d = np.array([2, 3, 4]) +product_all = arr_1d.prod() + +# 2D array example: +# [[1, 2], +# [5, 6]] +arr_2d = np.array([[1, 2], [5, 6]]) + +# Product along columns (axis=0): (1*5, 2*6) +product_cols = arr_2d.prod(axis=0) + +print(f"Product of all elements (1D): {product_all}") +print(f"Product along columns (axis=0) in 2D: {product_cols}") +``` + +The code above generates the following output: + +```shell +Product of all elements (1D): 24 +Product along columns (axis=0) in 2D: [ 5 12] +``` + +## Codebyte Example +The following codebyte example demonstrates calculating the product of elements along the rows`(axis=1)`of a 2D array: + +```codebyte/python +import numpy as np + +arr = np.array([[10, 1], + [3, 4]]) + +# Product along rows (axis=1): (10*1, 3*4) +product_rows = arr.prod(axis=1) + +print(product_rows) +``` From 3ab3e9d44d491c68cc971dd86d479f2a30662204 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 29 Oct 2025 13:14:32 +0530 Subject: [PATCH 2/3] Revise .prod() function documentation in prod.md Updated the description and parameters of the .prod() function in NumPy documentation for clarity and accuracy. Fixed formatting issues and improved examples. --- .../numpy/concepts/ndarray/terms/prod/prod.md | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/prod/prod.md b/content/numpy/concepts/ndarray/terms/prod/prod.md index 08afe69d8e1..8a4f8a70b61 100644 --- a/content/numpy/concepts/ndarray/terms/prod/prod.md +++ b/content/numpy/concepts/ndarray/terms/prod/prod.md @@ -6,34 +6,40 @@ Subjects: - 'Data Science' Tags: - 'Arrays' - - 'Mathematical Functions' - 'Functions' + - 'Math' - 'NumPy' CatalogContent: - 'learn-python-3' - 'paths/data-science' --- -The **`.prod()`** function in NumPy computes the product (multiplication) of all the elements in the array. This function is essential for mathematical and statistical operations and is particularly useful with multi-dimensional arrays where you might need to calculate the product across specific dimensions (rows or columns). The function is also available as the standalone function **`numpy.prod()`**. +The **`.prod()`** function in NumPy returns the product of array elements over a specified axis. It’s useful in mathematical and statistical operations, especially with multi-dimensional arrays where the product needs to be computed across rows, columns, or the entire array. This function is also available as the standalone function `numpy.prod()`. + ## Syntax -```psuedo +```pseudo ndarray.prod(axis=None, dtype=None, out=None, keepdims=False, initial=1, where=True) ``` -- `axis`: Optional. The axis or axes along which the product is computed. - If None (default), the product of all elements (the array is effectively flattened) is returned. - If an int (e.g., 0 or 1)the product is calculated along that axis. -- `dtype`:The data type of the returned product. By default, it's often inferred to prevent intermediate overflow, typically using the platform's default integer or float. -- `out`: Alternative output array in which to place the result. -- `keepdims `:If True, the reduced axis is retained in the result with a dimension size of 1. -- `initial `:The starting value for the product, which is multiplied by the product of the array elements. The default is 1. -- `where`:Elements corresponding to False in this array are excluded from the product. +**Parameters:** -## Example +- `axis` (int or tuple of ints, optional): Axis or axes along which the product is computed. + - If `None` (default), the product of all elements in the array is returned. + - If an integer or tuple, the product is computed along the specified axis or axes. +- `dtype` (data-type, optional): The data type of the returned array and of the accumulator used in the computation. If not specified, it’s inferred from the input to avoid overflow when possible. +- `out` (ndarray, optional): An alternate output array in which to place the result. It must have the same shape as the expected output. +- `keepdims` (bool, optional): If `True`, the reduced axes are left in the result as dimensions with size one. Default is `False`. +- `initial` (scalar, optional): The starting value for the product. The default is `1`. +- `where` (array_like of bool, optional): A boolean array indicating elements to include in the product. Elements where `where` is `False` are ignored. + +**Return value:** -The following example shows the use of the `.prod()`to calculate the product of all elements in a 1D array and the product along the columns in a 2D array: +Returns the product of array elements over the specified axis (or the entire array if `axis=None`). The returned type matches the input or the specified `dtype`. +## Example + +The following example shows the use of the `.prod()` to calculate the product of all elements in a 1D array and the product along the columns in a 2D array: ```py import numpy as np @@ -42,7 +48,7 @@ import numpy as np arr_1d = np.array([2, 3, 4]) product_all = arr_1d.prod() -# 2D array example: +# 2D array example: # [[1, 2], # [5, 6]] arr_2d = np.array([[1, 2], [5, 6]]) @@ -62,12 +68,13 @@ Product along columns (axis=0) in 2D: [ 5 12] ``` ## Codebyte Example -The following codebyte example demonstrates calculating the product of elements along the rows`(axis=1)`of a 2D array: + +The following codebyte example demonstrates calculating the product of elements along the rows `(axis=1)` of a 2D array: ```codebyte/python import numpy as np -arr = np.array([[10, 1], +arr = np.array([[10, 1], [3, 4]]) # Product along rows (axis=1): (10*1, 3*4) From 5a184db5206ab505946b1b31f1e945b0ff154804 Mon Sep 17 00:00:00 2001 From: Sriparno Roy <89148144+Sriparno08@users.noreply.github.com> Date: Fri, 31 Oct 2025 12:05:54 +0530 Subject: [PATCH 3/3] Minor changes --- content/numpy/concepts/ndarray/terms/prod/prod.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/prod/prod.md b/content/numpy/concepts/ndarray/terms/prod/prod.md index 8a4f8a70b61..c4063980e94 100644 --- a/content/numpy/concepts/ndarray/terms/prod/prod.md +++ b/content/numpy/concepts/ndarray/terms/prod/prod.md @@ -24,12 +24,12 @@ ndarray.prod(axis=None, dtype=None, out=None, keepdims=False, initial=1, where=T **Parameters:** -- `axis` (int or tuple of ints, optional): Axis or axes along which the product is computed. +- `axis` (int or tuple of ints, optional): The axis or axes along which the product is computed. - If `None` (default), the product of all elements in the array is returned. - - If an integer or tuple, the product is computed along the specified axis or axes. -- `dtype` (data-type, optional): The data type of the returned array and of the accumulator used in the computation. If not specified, it’s inferred from the input to avoid overflow when possible. + - If an integer or tuple of integers, the product is computed along the specified axis or axes. +- `dtype` (Optional): The data type of the returned array and of the accumulator used in the computation. If not specified, it’s inferred from the input to avoid overflow when possible. - `out` (ndarray, optional): An alternate output array in which to place the result. It must have the same shape as the expected output. -- `keepdims` (bool, optional): If `True`, the reduced axes are left in the result as dimensions with size one. Default is `False`. +- `keepdims` (bool, optional): If `True`, the reduced axes are kept in the result as dimensions with size one. Default is `False`. - `initial` (scalar, optional): The starting value for the product. The default is `1`. - `where` (array_like of bool, optional): A boolean array indicating elements to include in the product. Elements where `where` is `False` are ignored. @@ -48,7 +48,7 @@ import numpy as np arr_1d = np.array([2, 3, 4]) product_all = arr_1d.prod() -# 2D array example: +# 2D array example # [[1, 2], # [5, 6]] arr_2d = np.array([[1, 2], [5, 6]]) @@ -69,7 +69,7 @@ Product along columns (axis=0) in 2D: [ 5 12] ## Codebyte Example -The following codebyte example demonstrates calculating the product of elements along the rows `(axis=1)` of a 2D array: +The following codebyte example demonstrates calculating the product of elements along the rows (`axis=1`) of a 2D array: ```codebyte/python import numpy as np