Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double sort in numpy.median (Trac #295) #893

Closed
numpy-gitbot opened this issue Oct 19, 2012 · 0 comments
Closed

Double sort in numpy.median (Trac #295) #893

numpy-gitbot opened this issue Oct 19, 2012 · 0 comments

Comments

@numpy-gitbot
Copy link

Original ticket http://projects.scipy.org/numpy/ticket/295 on 2006-09-26 by @FrancescAlted, assigned to unknown.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant