|
| 1 | +--- |
| 2 | +Title: '.cumprod()' |
| 3 | +Description: 'Returns the cumulative product of array elements along a specified axis.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Linear Algebra' |
| 10 | + - 'Matrices' |
| 11 | + - 'NumPy' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/data-science' |
| 15 | +--- |
| 16 | + |
| 17 | +In NumPy, the **`.cumprod()`** method returns the cumulative product of the array elements over a particular axis. This method belongs to the `ndarray` class. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +ndarray.cumprod(axis=None, dtype=None, out=None) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `axis` (optional): Axis along which the cumulative product is computed. The default (None) flattens the array. |
| 28 | +- `dtype` (optional): The data type of the output array. Useful when input elements are of smaller types and may overflow. |
| 29 | +- `out` (optional): An alternative output array where results will be stored. It must have the same shape as the expected output. |
| 30 | + |
| 31 | +**Return value:** |
| 32 | + |
| 33 | +Returns a new array that contains the cumulative product of elements along the specified axis. If `out` is provided, the result is placed into it. |
| 34 | + |
| 35 | +## Example 1: Cumulative Product of a 1D Array |
| 36 | + |
| 37 | +This example computes the cumulative product of all elements in a one-dimensional array: |
| 38 | + |
| 39 | +```py |
| 40 | +import numpy as np |
| 41 | + |
| 42 | +a = np.array([1, 2, 3, 4]) |
| 43 | +print("Original array:", a) |
| 44 | + |
| 45 | +result = a.cumprod() |
| 46 | +print("Cumulative product:", result) |
| 47 | +``` |
| 48 | + |
| 49 | +The output of this code is: |
| 50 | + |
| 51 | +```shell |
| 52 | +Original array: [1 2 3 4] |
| 53 | +Cumulative product: [ 1 2 6 24] |
| 54 | +``` |
| 55 | + |
| 56 | +Each element in the output represents the product of all elements up to that index in the input array. |
| 57 | + |
| 58 | +## Example 2: Cumulative Product Along Different Axes |
| 59 | + |
| 60 | +This example shows how `.cumprod()` behaves when applied across rows and columns of a 2D array: |
| 61 | + |
| 62 | +```py |
| 63 | +import numpy as np |
| 64 | + |
| 65 | +b = np.array([[1, 2, 3], |
| 66 | + [4, 5, 6]]) |
| 67 | + |
| 68 | +print("Original array:") |
| 69 | +print(b) |
| 70 | + |
| 71 | +# Cumulative product along rows (axis=1) |
| 72 | +row_cumprod = b.cumprod(axis=1) |
| 73 | +print("\nCumulative product along rows (axis=1):") |
| 74 | +print(row_cumprod) |
| 75 | + |
| 76 | +# Cumulative product along columns (axis=0) |
| 77 | +col_cumprod = b.cumprod(axis=0) |
| 78 | +print("\nCumulative product along columns (axis=0):") |
| 79 | +print(col_cumprod) |
| 80 | +``` |
| 81 | + |
| 82 | +The output of this code is: |
| 83 | + |
| 84 | +```shell |
| 85 | +Original array: |
| 86 | +[[1 2 3] |
| 87 | + [4 5 6]] |
| 88 | + |
| 89 | +Cumulative product along rows (axis=1): |
| 90 | +[[ 1 2 6] |
| 91 | + [ 4 20 120]] |
| 92 | + |
| 93 | +Cumulative product along columns (axis=0): |
| 94 | +[[ 1 2 3] |
| 95 | + [ 4 10 18]] |
| 96 | +``` |
| 97 | + |
| 98 | +- When `axis=1`, the cumulative product is computed across each row. |
| 99 | +- When `axis=0`, it’s computed down each column. |
| 100 | + |
| 101 | +## Codebyte Example |
| 102 | + |
| 103 | +Run this interactive example to experiment with `.cumprod()` on a 2D array: |
| 104 | + |
| 105 | +```codebyte/python |
| 106 | +import numpy as np |
| 107 | +
|
| 108 | +a = np.array([[2, 3, 4], |
| 109 | + [5, 6, 7]]) |
| 110 | +
|
| 111 | +print("Original array:") |
| 112 | +print(a) |
| 113 | +
|
| 114 | +print("\nCumulative product (flattened):", a.cumprod()) |
| 115 | +print("\nCumulative product along axis=0:\n", a.cumprod(axis=0)) |
| 116 | +print("\nCumulative product along axis=1:\n", a.cumprod(axis=1)) |
| 117 | +``` |
0 commit comments