Standard Deviation One-liner
Alain Ketterlin
alain at dpt-info.u-strasbg.fr
Fri Jun 3 14:50:36 EDT 2011
Billy Mays <noway at nohow.com> writes:
> I'm trying to shorten a one-liner I have for calculating the standard
> deviation of a list of numbers. I have something so far, but I was
> wondering if it could be made any shorter (without imports).
> a=lambda d:(sum((x-1.*sum(d)/len(d))**2 for x in d)/(1.*(len(d)-1)))**.5
You should make it two half-liners, because this one repeatedly computes
sum(d). I would suggest:
aux = lambda s1,s2,n: (s2 - s1*s1/n)/(n-1)
sv = lambda d: aux(sum(d),sum(x*x for x in d),len(d))
(after some algebra). Completely untested, assumes data come in as
floats. You get the idea.
-- Alain.
More information about the Python-list
mailing list