Numpy cumulative product

import numpy as np

arr = np.array([[6, 1, 3], [8, 2, 1]])

print(‘2D Array\n’,arr)

b = np.cumprod(arr, dtype=int)

print(‘Cumulative product of all elements\n’,b)

c = np.cumprod(arr,axis=0)

print(‘Cumulative product of elements axis=0\n’,c)


x = np.array([6, 1, 3])

print(‘Single dimension array ‘,x)

y = np.cumprod(x, dtype=int)

print(‘Single Dimension array Cumulative product of all elements\n’,y)


Output

2D Array
[[6 1 3]
[8 2 1]]
Cumulative product of all elements
[ 6 6 18 144 288 288]
Cumulative product of elements axis=0
[[ 6 1 3]
[48 2 3]]
Single dimension array [6 1 3]
Single Dimension array Cumulative product of all elements
[ 6 6 18]

Published by Python programming examples for beginners

Abhay Gadkari is an IT professional having around experience of 20+ years in IT industry. He worked on web technologies and databases with Insurance and ERP projects.