Articles

cumsum() and cumprod() in numpy | numpy cumsum example

cumsum() and cumprod() in numpy | numpy cumsum example


In this numpy tutorial, we will discuss about:

  • numpy cumsum functions performed on the given numpy array
  • numpy cumprod functions performed on the given numpy array
  • numpy cumsum example
  • numpy cumprod example

Introduction to numpy cumsum and cumprod

numpy stands for numeric python which is used to perform mathematical operations on arrays.

It is a module in which we have to import from the python. 

Syntax to import:

import numpy

We can also use alias for the module 

For example,

import numpy as np

We can directly use np to call the numpy module.

 

Array

An array is an one dimensional data structure used to store single data type data.

I.E It will only store all integer data or all string type data.or all float type data.

We can create an numpy array by using array() function.

Syntax:

numpy.array(elements)

where, elements are the input data elements.


numpy cumsum()

numpy cumsum stands for cumulative sum. This function will return the cumulative sum for the elements in an given numpy array.

Syntax:

numpy.cumsum(array_data)

where, array_data is an input array

Internal Working of numpy cumsum:

array = [1,2,3,4,5]

1 => 1
2 => 1+2 = 3
3 => 1+2+3 = 6
4 => 1+2+3+4 = 10
5 => 1+2+3+4+5 = 15

Therefore the result is [ 1 3 6 10 15]

Examplenumpy cumsum example

In this numpy cumsum example, we are going to create two numpy arrays with 5 elements each and perform cumulative sum.

#importing numpy module
import numpy

#creating array1
arraydata1=numpy.array([1,2,3,4,5])

#creating array2
arraydata2=numpy.array([1,2,10,4,0])

#display two arrays
print(arraydata1,arraydata2)

#perform cumsum on  first array
print(numpy.cumsum(arraydata1))

#perform cumsum on  second array
print(numpy.cumsum(arraydata2))

Output: numpy cumsum output

[1 2 3 4 5] [ 1  2 10  4  0]
[ 1  3  6 10 15]
[ 1  3 13 17 17]

numpy cumprod()

numpy cumprod stands for cumulative product. This function will return the cumulative product for the elements in an given numpy array.

 

Syntax of numpy cumprod:

numpy.cumprod(array_data)

where, array_data is an input array

Internal Working of numpy cumprod:

array = [1,2,3,4,5]

1 => 1
2 => 1*2 = 2
3 => 1*2*3 = 6
4 => 1*2*3*4 = 24
5 => 1*2*3*4*5 = 120

Therefore the result is [ 1 2 6 24 120]

Examplenumpy cumprod example

In this numpy cumprod example, we are going to create two numpy arrays with 5 elements each and perform cumulative product.

#importing numpy module
import numpy

#creating array1
arraydata1=numpy.array([1,2,3,4,5])

#creating array2
arraydata2=numpy.array([1,2,10,4,0])

#display two arrays
print(arraydata1,arraydata2)

#perform cumprod on  first array
print(numpy.cumprod(arraydata1))

#perform cumprod on  second array
print(numpy.cumprod(arraydata2))

Output: numpy cumprod result

[1 2 3 4 5] [ 1  2 10  4  0]
[  1   2   6  24 120]
[ 1  2 20 80  0]

This concludes our session on numpy cumsum, np.cumsum example, numpy cumprod meaning, numpy cumprod example.


Numpy

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Feb 26,2023  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!