Finding subsets for a robust regression
Gerard flanagan
grflanagan at gmail.com
Mon Sep 29 16:46:53 EDT 2008
tkpmep at hotmail.com wrote:
>
> x1 = [] #unique instances of x and y
> y1 = [] #median(y) for each unique value of x
> for xx,yy in d.iteritems():
> x1.append(xx)
> l = len(yy)
> if l == 1:
> y1.append(yy[0])
> else:
> yy.sort()
> y1.append( (yy[l//2-1] + yy[l//2])/2.0 if l % 2 == 0 else
> yy[l//2] )
> --
Not tested, but is this equivalent?
x1 = []
y1 = []
for xx, yy in d.iteritems():
L = len(yy) // 2
yy.sort()
y1.append(yy[L])
if not L & 1:
y1[-1] = (y1[-1] + yy[L-1]) / 2.0
It means that you have a pointless 'sort' when len(yy) == 1, but then
you save an 'if' per-iteration.
G.
More information about the Python-list
mailing list