mean ans std dev of an array?

Paul McGuire ptmcg at austin.rr._bogus_.com
Tue Oct 24 21:12:14 CEST 2006


<skip at pobox.com> wrote in message 
news:mailman.1106.1161715405.11739.python-list at python.org...
>
>    >> n = len(a)
>    >> mean = sum(a) / n
>    >> sd = sqrt(sum((x-mean)**2 for x in a) / n)
>    ...
>    >> If there is a faster way... like transferring the array to a
>    >> different container class...  but what?
>
> Perhaps:
>
>    >>> import scipy
>    >>> print scipy.mean([1,2,3,4,5,6])
>    3.5
>    >>> print scipy.std([1,2,3,4,5,6])
>    1.87082869339
>
> Skip

Can scipy work with an iterator/generator?  If you can only make one pass 
through the data, you can try this:

lst = (random.gauss(0,10) for i in range(1000))
# compute n, sum(x), and sum(x**2) with single pass through list
n,sumx,sumx2 = reduce(lambda a,b:(a[0]+b[0],a[1]+b[1],a[2]+b[2]), ((1,x,x*x) 
for x in lst) )
sd = sqrt( (sumx2 - (sumx*sumx/n))/(n-1) )

-- Paul





More information about the Python-list mailing list