[Numpy-discussion] A faster median (Wirth's method)
Citi, Luca
lciti at essex.ac.uk
Wed Sep 2 04:50:03 EDT 2009
Hello Sturla,
I had a quick look at your code.
Looks fine.
A few notes...
In "select" you should replace numpy with np.
In "_median" how can you, if n==2, use s[] if s is not defined?
What if n==1?
Also, I think when returning an empty array, it should be of
the same type you would get in the other cases.
You could replace _median with the following.
Best,
Luca
def _median(x, inplace):
assert(x.ndim == 1)
n = x.shape[0]
if n > 2:
k = n >> 1
s = select(x, k, inplace=inplace)
if n & 1:
return s[k]
else:
return 0.5*(s[k]+s[:k].max())
elif n == 0:
return np.empty(0, dtype=x.dtype)
elif n == 2:
return 0.5*(x[0]+x[1])
else: # n == 1
return x[0]
More information about the NumPy-Discussion
mailing list