Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the cumsum function in NumPy decay while adding?

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?

like image 928
D_C_A Avatar asked Mar 07 '15 12:03

D_C_A


People also ask

How does Numpy Cumsum work?

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.

What does the Cumsum function do?

The cumsum() function in R computes the cumulative sum of elements in a vector object.

What does Numpy Cumsum return?

cumsum. Return the cumulative sum of the elements along a given axis.

What does Cumsum mean in Python?

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.


1 Answers

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])
like image 165
Josh Bode Avatar answered Sep 28 '22 09:09

Josh Bode