Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
numpy
python
communitycreator

What is the numpy.cumsum() function in NumPy?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The numpy.cumsum() function in NumPy is used to compute the cumulative sum of elements in a given input array over a given axis.

Syntax

numpy.cumsum(a, axis=None, dtype=None, out=None)

Parameter value

The numpy.cumsum() function takes the following parameter values:

  • a (required): The input array containing numbers is to be computed.
  • axis(optional): The axis along which the product is determined.
  • dtype (optional): The data type of the output array.
  • out (optional): The alternate array where the result is placed.

Return value

The numpy.cumsum() function returns an output array holding the result.

Example

import numpy as np
# creating an array
x = np.array([1, 2, 3])
# Implementing the numpy.cumsum() function
myarray = np.cumsum(x, axis=0)
print(x)
print(myarray)
Implementing the numpy.cumsum() function

Explanation

Here is a line-by-line explanation of the above code:

  • Line 1: We import the numpy module.
  • Line 4: We create an array, x, using the array() method.
  • Line 7: We implement the np.cumsum() function on the array. The result is assigned to a variable, myarray.
  • Line 9: We print the input array x.
  • Line 10: We print the variable myarray.

RELATED TAGS

function
numpy
python
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring