From cc9c34783509f909f6adfbd0d47544ed3aae71d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Szkil?= Date: Wed, 29 Oct 2025 19:15:38 +0100 Subject: [PATCH 1/5] [Term Entry] Python .cumsum() --- .../concepts/ndarray/terms/cumsum/cumsum.md | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 content/numpy/concepts/ndarray/terms/cumsum/cumsum.md diff --git a/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md b/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md new file mode 100644 index 00000000000..c8300eff41f --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md @@ -0,0 +1,128 @@ +--- +Title: '.cumsum()' +Description: 'Returns the cumulative sum of the elements along the given axis.' +Subjects: + - 'Code Foundations' + - 'Data Science' +Tags: + - 'Arrays' + - 'Methods' + - 'Numpy' + - 'Python' +CatalogContent: + - 'learn-python-3' + - 'paths/data-science' +--- + +The **`.cumsum()`** method in NumPy returns the cumulative sum of the elements along a specified axis of an array. If no axis is specified, the method computes the cumulative sum of the flattened array. + +## Syntax + +```pseudo +ndarray.cumsum(axis=None, dtype=None, out=None) +``` + +**Parameters:** + +- `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. +- `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. +- `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. + +**Return value:** + +Returns an array containing the result if `out` is not specified. If `out` is specified, a reference to `out` is returned. + +## Example 1: No Axis Specified + +In this example, the cumulative sum is calculated without specifying an axis, resulting in a flattened array: + +```py +import numpy as np + +array = np.array([[1, 2, 3], [4, 5, 6]]) +cumsum_result = array.cumsum() +print(cumsum_result) +``` + +The output of this code will be: + +```shell +[ 1 3 6 10 15 21] +``` + +## Example 2: Specified Axis + +In this next example, an axis to compute the cumulative sum is specified: + +```py +import numpy as np + +array = np.array([[1, 2, 3], [4, 5, 6]]) +cumsum_result_0 = array.cumsum(axis=0) +cumsum_result_1 = array.cumsum(axis=1) +print(cumsum_result_0) +print(cumsum_result_1) +``` + +The output will be: + +```shell +[[1 2 3] + [5 7 9]] + +[[ 1 3 6] + [ 4 9 15]] +``` + +## Example 3: Specified Data Type + +In this one, the data type for the cumulative sum result is specified: + +```py +import numpy as np + +array = np.array([1, 2, 3]) +cumsum_result = array.cumsum(dtype=float) +print(cumsum_result) +``` + +The output array will be: + +```shell +[1. 3. 6.] +``` + +As it can be seen, the cumulative sum values are represented as floating-point numbers this time. + +## Example 4: Output Array Provided + +In the following example, an empty output array is provided to store the cumulative sum result: + +```py +import numpy as np + +array = np.array([1, 2, 3]) +output_array = np.empty_like(array) +array.cumsum(out=output_array) +print(output_array) +``` + +The output will be: + +```shell +[1 3 6] +``` + +## Codebyte Example + +In this codebyte example, an array is created and the cumulative sum is calculated along both axes while also specifying the type of the output: + +```codebyte/python +import numpy as np + +array = np.array([[1, 2], [3, 4]]) +cumsum_result_0 = array.cumsum(axis=0, dtype=int) +cumsum_result_1 = array.cumsum(axis=1, dtype=float) +print(cumsum_result_0) +print(cumsum_result_1) +``` From 86bab7e560a925b8e2e84c7412f77c21c5aae506 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 30 Oct 2025 11:32:07 +0530 Subject: [PATCH 2/5] Revise .cumsum() documentation for clarity Updated the description and examples for the .cumsum() method in NumPy documentation to improve clarity and accuracy. --- .../concepts/ndarray/terms/cumsum/cumsum.md | 104 +++++++++--------- 1 file changed, 50 insertions(+), 54 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md b/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md index c8300eff41f..edfb400f6b5 100644 --- a/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md +++ b/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md @@ -1,6 +1,6 @@ --- Title: '.cumsum()' -Description: 'Returns the cumulative sum of the elements along the given axis.' +Description: 'Computes the cumulative sum of array elements along a specified axis.' Subjects: - 'Code Foundations' - 'Data Science' @@ -14,7 +14,7 @@ CatalogContent: - 'paths/data-science' --- -The **`.cumsum()`** method in NumPy returns the cumulative sum of the elements along a specified axis of an array. If no axis is specified, the method computes the cumulative sum of the flattened array. +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. ## Syntax @@ -30,99 +30,95 @@ ndarray.cumsum(axis=None, dtype=None, out=None) **Return value:** -Returns an array containing the result if `out` is not specified. If `out` is specified, a reference to `out` is returned. +Returns an array containing the cumulative sum of elements, or a reference to `out` if specified. -## Example 1: No Axis Specified +## Example 1: Computing Cumulative Sum with and without Axis -In this example, the cumulative sum is calculated without specifying an axis, resulting in a flattened array: +This example calculates the cumulative sum of a 2D array: ```py import numpy as np array = np.array([[1, 2, 3], [4, 5, 6]]) -cumsum_result = array.cumsum() -print(cumsum_result) -``` - -The output of this code will be: -```shell -[ 1 3 6 10 15 21] -``` +# Cumulative sum of flattened array +cumsum_flat = array.cumsum() -## Example 2: Specified Axis +# Cumulative sum along rows (axis=1) +cumsum_axis1 = array.cumsum(axis=1) -In this next example, an axis to compute the cumulative sum is specified: +# Cumulative sum along columns (axis=0) +cumsum_axis0 = array.cumsum(axis=0) -```py -import numpy as np - -array = np.array([[1, 2, 3], [4, 5, 6]]) -cumsum_result_0 = array.cumsum(axis=0) -cumsum_result_1 = array.cumsum(axis=1) -print(cumsum_result_0) -print(cumsum_result_1) +print("Flattened cumulative sum:\n", cumsum_flat) +print("\nCumulative sum along rows:\n", cumsum_axis1) +print("\nCumulative sum along columns:\n", cumsum_axis0) ``` -The output will be: +The output of this code is: ```shell -[[1 2 3] - [5 7 9]] +Flattened cumulative sum: +[ 1 3 6 10 15 21] +Cumulative sum along rows: [[ 1 3 6] [ 4 9 15]] -``` -## Example 3: Specified Data Type - -In this one, the data type for the cumulative sum result is specified: - -```py -import numpy as np - -array = np.array([1, 2, 3]) -cumsum_result = array.cumsum(dtype=float) -print(cumsum_result) +Cumulative sum along columns: +[[1 2 3] + [5 7 9]] ``` -The output array will be: - -```shell -[1. 3. 6.] -``` +Here: -As it can be seen, the cumulative sum values are represented as floating-point numbers this time. +- When no axis is specified, NumPy flattens the array before computing the cumulative sum. +- When an axis is specified, it computes the sum along the given dimension. -## Example 4: Output Array Provided +## Example 2: Using dtype and Output Array -In the following example, an empty output array is provided to store the cumulative sum result: +In this example, custom data type (`float`) is specified for the cumulative sum, and the result is stored in a preallocated output array: ```py import numpy as np array = np.array([1, 2, 3]) -output_array = np.empty_like(array) -array.cumsum(out=output_array) + +# Create an empty array for output +output_array = np.empty_like(array, dtype=float) + +# Compute cumulative sum with custom dtype and output +array.cumsum(dtype=float, out=output_array) + +print("Cumulative sum with float dtype:") print(output_array) ``` -The output will be: +The output of this code is: ```shell -[1 3 6] +Cumulative sum with float dtype: +[1. 3. 6.] ``` ## Codebyte Example -In this codebyte example, an array is created and the cumulative sum is calculated along both axes while also specifying the type of the output: +In this codebyte example, the cumulative sum of a 2D array is computed along both axes using different data types for each: ```codebyte/python import numpy as np array = np.array([[1, 2], [3, 4]]) -cumsum_result_0 = array.cumsum(axis=0, dtype=int) -cumsum_result_1 = array.cumsum(axis=1, dtype=float) -print(cumsum_result_0) -print(cumsum_result_1) + +# Cumulative sum along rows (axis=1) with float dtype +cumsum_row = array.cumsum(axis=1, dtype=float) + +# Cumulative sum along columns (axis=0) with int dtype +cumsum_col = array.cumsum(axis=0, dtype=int) + +print("Cumulative sum along rows (float):") +print(cumsum_row) + +print("\nCumulative sum along columns (int):") +print(cumsum_col) ``` From ebd8779b2e6fd4e6435a0a9ec3bb2834b6aa0318 Mon Sep 17 00:00:00 2001 From: Radhika-okhade Date: Thu, 30 Oct 2025 12:07:43 +0530 Subject: [PATCH 3/5] Update content/numpy/concepts/ndarray/terms/cumsum/cumsum.md --- content/numpy/concepts/ndarray/terms/cumsum/cumsum.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md b/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md index edfb400f6b5..65d98571079 100644 --- a/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md +++ b/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md @@ -7,7 +7,7 @@ Subjects: Tags: - 'Arrays' - 'Methods' - - 'Numpy' + - 'NumPy' - 'Python' CatalogContent: - 'learn-python-3' From 033093cd08665776b1031e0101c700f23ae2f250 Mon Sep 17 00:00:00 2001 From: Radhika-okhade Date: Thu, 30 Oct 2025 12:07:54 +0530 Subject: [PATCH 4/5] Update content/numpy/concepts/ndarray/terms/cumsum/cumsum.md --- content/numpy/concepts/ndarray/terms/cumsum/cumsum.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md b/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md index 65d98571079..9795ca73fc7 100644 --- a/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md +++ b/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md @@ -77,7 +77,7 @@ Here: ## Example 2: Using dtype and Output Array -In this example, custom data type (`float`) is specified for the cumulative sum, and the result is stored in a preallocated output array: +In this example, a custom data type (`float`) is specified for the cumulative sum, and the result is stored in a preallocated output array: ```py import numpy as np From e3747ed8164c190305be428ad2a0a3c002b9d347 Mon Sep 17 00:00:00 2001 From: Radhika-okhade Date: Thu, 30 Oct 2025 12:08:01 +0530 Subject: [PATCH 5/5] Update content/numpy/concepts/ndarray/terms/cumsum/cumsum.md --- content/numpy/concepts/ndarray/terms/cumsum/cumsum.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md b/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md index 9795ca73fc7..2443ee442b2 100644 --- a/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md +++ b/content/numpy/concepts/ndarray/terms/cumsum/cumsum.md @@ -103,7 +103,7 @@ Cumulative sum with float dtype: ## Codebyte Example -In this codebyte example, the cumulative sum of a 2D array is computed along both axes using different data types for each: +In this example, the cumulative sum of a 2D array is computed along both axes using different data types for each: ```codebyte/python import numpy as np