[Numpy-discussion] Is there a way to reset an accumulate function?

Cera, Tim tim at cerazone.net
Wed Oct 24 21:24:03 EDT 2012


On Wed, Oct 24, 2012 at 4:47 AM, Robert Kern <robert.kern at gmail.com> wrote:

> How about this?
>
>
> def nancumsum(x):
>     nans = np.isnan(x)
>     x = np.array(x)
>     x[nans] = 0
>     reset_idx = np.zeros(len(x), dtype=int)
>     reset_idx[nans] = np.arange(len(x))[nans]
>     reset_idx = np.maximum.accumulate(reset_idx)
>     cumsum = np.cumsum(x)
>     cumsum = cumsum - cumsum[reset_idx]
>     return cumsum
>

Thank you for putting in the time to look at this.

It doesn't work for the first group of numbers if x[0] is non-zero.  Could
perhaps concatenate a np.nan at the beginning to force a reset and adjust
the returned array to not include the dummy value...

def nancumsum(x):
    x = np.concatenate(([np.nan], x))
    nans = np.isnan(x)
    x = np.array(x)
    x[nans] = 0
    reset_idx = np.zeros(len(x), dtype=int)
    reset_idx[nans] = np.arange(len(x))[nans]
    reset_idx = np.maximum.accumulate(reset_idx)
    cumsum = np.cumsum(x)
    cumsum = cumsum - cumsum[reset_idx]
    return cumsum[1:]

>>> a
array([  4.,   1.,   2.,   0.,  18.,   5.,   6.,   0.,   8.,   9.],
dtype=float32)

If no np.nan, then 'nancumsum' and 'np.cumsum' should be the same...

>>> np.cumsum(a)
array([  4.,   5.,   7.,   7.,  25.,  30.,  36.,  36.,  44.,  53.],
dtype=float32)

>>> nancumsum(a)
array([  4.,   5.,   7.,   7.,  25.,  30.,  36.,  36.,  44.,  53.])

>>> a[3] = np.nan

>>> np.cumsum(a)
array([  4.,   5.,   7.,  nan,  nan,  nan,  nan,  nan,  nan,  nan],
dtype=float32)

>>> nancumsum(a)
array([  4.,   5.,   7.,   0.,  18.,  23.,  29.,  29.,  37.,  46.])

Excellent!

Kindest regards,
Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121024/8067d1fc/attachment.html>


More information about the NumPy-Discussion mailing list