# [Numpy-discussion] Can this be done more efficiently using numpy?

Vishal Rana ranavishal@gmail....
Thu Apr 1 18:55:14 CDT 2010

```Thanks Didrik!

On Thu, Apr 1, 2010 at 1:21 AM, Didrik Pinte <lists@dipole-consulting.com>wrote:

> On Wed, 2010-03-31 at 23:13 -0700, Vishal Rana wrote:
> > Hi,
> >
> >
> >  A calculation which goes like this...
> >
> >
> > n = 5
> > a = np.arange(1000)
> > b = np.arange(n - 1, 1000)
> >
> >
> > l = []
> > for i in range(b.size):
> >     # Absolute difference of n a elements and nth b element
> >     x = np.abs(a[i:i + n] - b[i])
> >
> >     # Average of x
> >     y = x.sum() / n
> >
> >     l.append(y)
> >
> >
> > It takes a while if I have like 200K records!
> > Is there a more efficient way to do this using numpy?
>
> Like this ?
>
> import numpy
> from numpy.lib.stride_tricks import as_strided
> n = 5
> a = numpy.arange(1000)
> b = numpy.arange(n - 1, 1000)
>
>
> def sum_1(n, a, b):
>    # original method
>     l = []
>    for i in range(b.size):
>        # Absolute difference of n a elements and nth b element
>         x = numpy.abs(a[i:i + n] - b[i])
>
>        # Average of x
>        y = x.sum() / n
>
>        l.append(y)
>     return l
>
> def sum_numpy(n, a, b):
>    ast = as_strided(a, shape=(1000-n+1, n), strides=(8,8))
>    diff = numpy.abs(ast - b[:,None])
>    y = diff.sum(axis=1) /n
>    return y
>
> test1 = sum_1(n,a,b)
> test2 = sum_numpy(n,a,b)
>
> assert(numpy.alltrue(test2 == test1))
>
> -- Didrik
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100401/fd3dc495/attachment.html
```