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

Didrik Pinte lists@dipole-consulting....
Thu Apr 1 03:21:01 CDT 2010

```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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
Url : http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100401/0306d113/attachment.bin
```