|
| 1 | +--- |
| 2 | +Title: '.cumsum()' |
| 3 | +Description: 'Computes the cumulative sum of array elements along a specified axis.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Methods' |
| 10 | + - 'NumPy' |
| 11 | + - 'Python' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/data-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`.cumsum()`** method in NumPy computes the cumulative sum of elements along a specified axis. If no axis is provided, it returns the cumulative sum of the flattened array. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +ndarray.cumsum(axis=None, dtype=None, out=None) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `axis` (Optional): Specifies the axis along which the cumulative sum is computed. If the axis is not specified, the cumsum is computed over the flattened array. |
| 28 | +- `dtype` (Optional): Specifies the data type of the returned array. If not provided, it defaults to the data type of the input array. However, if the array has an integer type with a precision lower than that of the default platform integer, the default platform integer is used instead. |
| 29 | +- `out` (Optional): An optional output array in which to store the resulting cumulative sum. The shape and buffer length must match that of the expected output but the type will be cast if necessary. |
| 30 | + |
| 31 | +**Return value:** |
| 32 | + |
| 33 | +Returns an array containing the cumulative sum of elements, or a reference to `out` if specified. |
| 34 | + |
| 35 | +## Example 1: Computing Cumulative Sum with and without Axis |
| 36 | + |
| 37 | +This example calculates the cumulative sum of a 2D array: |
| 38 | + |
| 39 | +```py |
| 40 | +import numpy as np |
| 41 | + |
| 42 | +array = np.array([[1, 2, 3], [4, 5, 6]]) |
| 43 | + |
| 44 | +# Cumulative sum of flattened array |
| 45 | +cumsum_flat = array.cumsum() |
| 46 | + |
| 47 | +# Cumulative sum along rows (axis=1) |
| 48 | +cumsum_axis1 = array.cumsum(axis=1) |
| 49 | + |
| 50 | +# Cumulative sum along columns (axis=0) |
| 51 | +cumsum_axis0 = array.cumsum(axis=0) |
| 52 | + |
| 53 | +print("Flattened cumulative sum:\n", cumsum_flat) |
| 54 | +print("\nCumulative sum along rows:\n", cumsum_axis1) |
| 55 | +print("\nCumulative sum along columns:\n", cumsum_axis0) |
| 56 | +``` |
| 57 | + |
| 58 | +The output of this code is: |
| 59 | + |
| 60 | +```shell |
| 61 | +Flattened cumulative sum: |
| 62 | +[ 1 3 6 10 15 21] |
| 63 | + |
| 64 | +Cumulative sum along rows: |
| 65 | +[[ 1 3 6] |
| 66 | + [ 4 9 15]] |
| 67 | + |
| 68 | +Cumulative sum along columns: |
| 69 | +[[1 2 3] |
| 70 | + [5 7 9]] |
| 71 | +``` |
| 72 | + |
| 73 | +Here: |
| 74 | + |
| 75 | +- When no axis is specified, NumPy flattens the array before computing the cumulative sum. |
| 76 | +- When an axis is specified, it computes the sum along the given dimension. |
| 77 | + |
| 78 | +## Example 2: Using dtype and Output Array |
| 79 | + |
| 80 | +In this example, a custom data type (`float`) is specified for the cumulative sum, and the result is stored in a preallocated output array: |
| 81 | + |
| 82 | +```py |
| 83 | +import numpy as np |
| 84 | + |
| 85 | +array = np.array([1, 2, 3]) |
| 86 | + |
| 87 | +# Create an empty array for output |
| 88 | +output_array = np.empty_like(array, dtype=float) |
| 89 | + |
| 90 | +# Compute cumulative sum with custom dtype and output |
| 91 | +array.cumsum(dtype=float, out=output_array) |
| 92 | + |
| 93 | +print("Cumulative sum with float dtype:") |
| 94 | +print(output_array) |
| 95 | +``` |
| 96 | + |
| 97 | +The output of this code is: |
| 98 | + |
| 99 | +```shell |
| 100 | +Cumulative sum with float dtype: |
| 101 | +[1. 3. 6.] |
| 102 | +``` |
| 103 | + |
| 104 | +## Codebyte Example |
| 105 | + |
| 106 | +In this example, the cumulative sum of a 2D array is computed along both axes using different data types for each: |
| 107 | + |
| 108 | +```codebyte/python |
| 109 | +import numpy as np |
| 110 | +
|
| 111 | +array = np.array([[1, 2], [3, 4]]) |
| 112 | +
|
| 113 | +# Cumulative sum along rows (axis=1) with float dtype |
| 114 | +cumsum_row = array.cumsum(axis=1, dtype=float) |
| 115 | +
|
| 116 | +# Cumulative sum along columns (axis=0) with int dtype |
| 117 | +cumsum_col = array.cumsum(axis=0, dtype=int) |
| 118 | +
|
| 119 | +print("Cumulative sum along rows (float):") |
| 120 | +print(cumsum_row) |
| 121 | +
|
| 122 | +print("\nCumulative sum along columns (int):") |
| 123 | +print(cumsum_col) |
| 124 | +``` |
0 commit comments