[Numpy-discussion] sort method raises unexpected error with axis=None

Anne Archibald peridot.faceted@gmail....
Tue Feb 12 19:56:59 CST 2008

On 12/02/2008, Matthew Brett <matthew.brett@gmail.com> wrote:

> Is it possible, in fact, to do an inplace sort on an array with
> axis=None (ie flat sort)?

It is, sometimes; just make an array object to point to the flattened
version and sort that:

In [16]: b = a[:]

In [17]: b.shape = (16,)

In [18]: b.sort()

This is not always possible, depending on the arrangement of a in memory.

An efficient way to handle in-place (or out-of-place, come to think of
it) median along multiple axes is actually to take medians along all
axes in succession. That saves you some sorting effort, and some
programming effort, and doesn't require in-place multidimensional

In [24]: def all_axes_median(a):
   ....:     if len(a.shape)>1:
   ....:         return all_axes_median(N.median(a))
   ....:     else:
   ....:         return N.median(a)

In [26]: all_axes_median(N.reshape(N.arange(32),(2,4,2,-1)))
Out[26]: 15.5


More information about the Numpy-discussion mailing list