You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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:
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.
Feel free to apply whatever patch you consider more appropriate.
The text was updated successfully, but these errors were encountered: