[Numpy-discussion] custom accumlators
Karol Langner
karol.langner at kn.pl
Fri Jan 5 11:18:46 CST 2007
On Friday 05 of January 2007 17:42, Matt Knox wrote:
> ---------------------------------------------
> Example 1 - exponential moving average:
>
> # naive brute force method...
> def expmave(x, k):
> result = numpy.array(x, copy=True)
> for i in range(1, result.size):
> result[i] = result[i-1] + k * (result[i] - result[i-1])
> return result
>
> # slicker method (if it worked, which it doesn't)...
> def expmave(x, k):
> def expmave_sub(a, b):
> return a + k * (b - a)
> return numpy.vectorize(expmave_sub).accumulate(x)
Why can't you simply use list comprehensions? Too slow?
For example:
def expmave(x, k):
return [x[0]] + [x[i-1] + k*(x[i]-x[i-1]) for i in range(1,len(x))]
Karol
--
written by Karol Langner
Fri Jan 5 17:58:27 CET 2007
More information about the Numpy-discussion
mailing list