[Numpy-discussion] moving window product

Keith Goodman kwgoodman@gmail....
Mon Mar 21 12:19:09 CDT 2011


On Mon, Mar 21, 2011 at 10:10 AM, Brent Pedersen <bpederse@gmail.com> wrote:
> hi, is there a way to take the product along a 1-d array in a moving
> window? -- similar to convolve, with product in place of sum?
> currently, i'm column_stacking the array with offsets of itself into
> window_size columns and then taking the product at axis 1.
> like::
>
>  w = np.column_stack(a[i:-window_size+i] for i in range(0, window_size))
>  window_product = np.product(w, axis=1)
>
> but then there are the edge effects/array size issues--like those
> handled in np.convolve.
> it there something in numpy/scipy that addresses this. or that does
> the column_stacking with an offset?

The Bottleneck package has a fast moving window sum (bn.move_sum and
bn.move_nansum). You could use that along with

>> a = np.random.rand(5)
>> a.prod()
   0.015877866878931741
>> np.exp(np.log(a).sum())
   0.015877866878931751

Or you could use strides or scipy.ndimage as in
https://github.com/kwgoodman/bottleneck/blob/master/bottleneck/slow/move.py


More information about the NumPy-Discussion mailing list