[Numpy-tickets] [NumPy] #295: Double sort in numpy.median
NumPy
numpy-tickets at scipy.net
Tue Sep 26 11:19:25 CDT 2006
#295: Double sort in numpy.median
-----------------------+----------------------------------------------------
Reporter: faltet | Owner: somebody
Type: defect | Status: new
Priority: normal | Milestone: 1.0 Release
Component: numpy.lib | Version: devel
Severity: normal | Keywords:
-----------------------+----------------------------------------------------
numpy.median is computing a sort twice for arrays with an even number of
elements. The next patch is a cure for this:
{{{
--- ./numpy/lib/function_base.py 2006-09-26 10:05:27.000000000
+0200
+++ /usr/local/lib/python2.5/site-packages/numpy/lib/function_base.py
2006-09-26 18:13:14.000000000 +0200
@@ -1046,11 +1046,10 @@
"""median(m) returns a median of m along the first dimension of m.
"""
sorted = msort(m)
+ index = sorted.shape[0]/2
if sorted.shape[0] % 2 == 1:
- return sorted[int(sorted.shape[0]/2)]
+ return sorted[index]
else:
- sorted = msort(m)
- index = sorted.shape[0]/2
return (sorted[index-1]+sorted[index])/2.0
}}}
Also, and although median shouldn't have to be well defined in other
values different from numbers, it can be forced to work decently for
string, object and boolean arrays. Behind is a patch for this.
{{{
--- ./numpy/lib/function_base.py 2006-09-26 10:05:27.000000000
+0200
+++ /usr/local/lib/python2.5/site-packages/numpy/lib/function_base.py
2006-09-26 18:13:27.000000000 +0200
@@ -1046,11 +1046,10 @@
"""median(m) returns a median of m along the first dimension of m.
"""
sorted = msort(m)
- if sorted.shape[0] % 2 == 1:
- return sorted[int(sorted.shape[0]/2)]
+ index = sorted.shape[0]/2
+ if sorted.shape[0] % 2 == 1 or sorted.dtype.kind in ["S", "O", "b"]:
+ return sorted[index]
else:
- sorted = msort(m)
- index = sorted.shape[0]/2
return (sorted[index-1]+sorted[index])/2.0
}}}
Feel free to apply whatever patch you consider more appropriate.
--
Ticket URL: <http://projects.scipy.org/scipy/numpy/ticket/295>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.
More information about the Numpy-tickets
mailing list