I have an array of values a = (2,3,0,0,4,3)
y=0
for x in a:
y = (y+x)*.95
Is there any way to use cumsum
in numpy
and apply the .95 decay to each row before adding the next value?
Python numpy cumsum() syntax The array can be ndarray or array-like objects such as nested lists. The axis parameter defines the axis along which the cumulative sum is calculated. If the axis is not provided then the array is flattened and the cumulative sum is calculated for the result array.
The cumsum() function in R computes the cumulative sum of elements in a vector object.
cumsum. Return the cumulative sum of the elements along a given axis.
cumsum() function is used when we want to compute the cumulative sum of array elements over a given axis. Syntax : numpy.cumsum(arr, axis=None, dtype=None, out=None) Parameters : arr : [array_like] Array containing numbers whose cumulative sum is desired.
Numba provides an easy way to vectorize
a function, creating a universal function (thus providing ufunc.accumulate
):
import numpy
from numba import vectorize, float64
@vectorize([float64(float64, float64)])
def f(x, y):
return 0.95 * (x + y)
>>> a = numpy.array([2, 3, 0, 0, 4, 3])
>>> f.accumulate(a)
array([ 2. , 4.75 , 4.5125 , 4.286875 ,
7.87253125, 10.32890469])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With